206. Reverse Linked List
Reverse a singly linked list.
Approach: Linked List (The Three-Pointer Technique)
We use three pointers to reverse the linked list: prev, curr, and temp. We iterate through the list, updating the next pointer of each node to point to its previous node:
- Prev (prev): Keep track of the previous node
- Curr: Keep track of the current node
- Next (temp): Keep track of the next node
Iterate through the current node in linked list:
- Store the next node into the temp
- Reverse: Update the next to point to previous node
- Update the position for previous node and current node
null -> 1 -> 2 -> 3
prev curr
curr->next = null
then move prev and curr, prev will always be head# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head
prev = None
curr = head
while curr:
temp = curr.next
curr.next = prev
prev = curr
curr = temp
return prev- Time: — we visit each node exactly once.
- Space: — we only use a constant amount of extra space.
Last updated on