LeetCode: Reverse Linked List
Reverse a singly linked list.
See original problem on leetcode.com
Difficulty: Medium
Solution
Three-pointer technique with previous, current, and next.
While current is not None:
- Store
current.nextinnext. - Set
current.nexttoprevious. - Move
previoustocurrent. - Move
currenttonext.
Runtime
0 ms | Beats 100.00%
class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: previous, current = None, head
while current: next = current.next current.next = previous previous, current = current, next
return previous