Reverse Linked List —
Three pointers. Every link rewired.
Reversing a linked list rewires every pointer — each node that pointed forward must point backward. Three pointers (prev, curr, next) walk the list together, flipping one link per step. Watch each pointer advance and each arrow reverse on LearnBug and the algorithm becomes unforgettable.
What is a Linked List Reversal?
A singly linked list has nodes where each points to the next. To reverse it, every node.next must point to the previous node instead of the next. The challenge: once you redirect curr.next, you lose the reference to the rest of the list. That's why next is saved before any pointer is changed — three pointers, one step at a time.
Why visualization changes everything
The three-pointer dance is easy to describe but hard to trace mentally. On LearnBug, prev, curr, and next are highlighted on the actual node diagram at every step. You watch curr.next flip from right to left, then all three pointers slide one position forward. The O(1) space in-place reversal becomes obvious.
Reversing 1 → 2 → 3 → 4 → None
At each step: save next, flip link, advance prev and curr.
prev = None, curr = head (node 1)
prev starts as None — it will become the new tail. curr starts at head.
Save next=2. Flip: 1.next = None (prev). Advance prev=1, curr=2.
Node 1 now points to None (it's the new tail). prev and curr both advance one step right.
Save next=3. Flip: 2.next = 1 (prev). Advance prev=2, curr=3.
Node 2 now points back to 1. The reversed chain grows: None ← 1 ← 2.
Flip 3→2, then flip 4→3. curr becomes None → loop ends.
After processing nodes 3 and 4, the full chain is reversed. prev now points to node 4 — the new head.
Return prev — node 4 is the new head ✓
Iterative and recursive approaches
def reverse_list(head):
prev = None
curr = head
while curr:
next_node = curr.next # 1. save next
curr.next = prev # 2. flip the link
prev = curr # 3. advance prev
curr = next_node # 4. advance curr
return prev # prev is now the new headdef reverse_list_recursive(head):
if not head or not head.next:
return head # base case — 0 or 1 node
new_head = reverse_list_recursive(head.next) # recurse to end
head.next.next = head # flip the link back
head.next = None # break forward link
return new_head # new head bubbles up unchangedTime & Space Complexity
Frequently asked questions
Why do we save next_node before flipping the link?
Once you execute curr.next = prev, the original forward link is gone. If you haven't saved it, you've lost your only reference to the rest of the list — you can never advance curr forward. Saving next_node = curr.next before the flip preserves that reference.
Why does prev end up as the new head?
The loop runs while curr is not None. The last iteration processes the original tail node, sets its .next to prev (the second-to-last), and then prev advances to the tail. When curr becomes None, the loop exits with prev pointing to the original tail — which is now the head of the reversed list.
How do you reverse only part of a linked list?
Walk to the start position, then apply the three-pointer reversal for exactly k steps, keeping track of the node before the reversal start (to reconnect) and the node after it ends (to link the reversed segment back in). This is LeetCode 92 — Reverse Linked List II.
What is the LeetCode problem for this?
LeetCode 206 — Reverse Linked List. One of the most fundamental linked list problems. Variants include: LC 92 (Reverse between positions m and n), LC 25 (Reverse Nodes in k-Group), and LC 234 (Palindrome Linked List — reverses the second half). Mastering the iterative three-pointer technique is essential for all of these.
Can the recursive approach cause a stack overflow?
Yes — for very long lists (tens of thousands of nodes), the recursive version uses O(n) call stack frames. Python's default recursion limit is 1000. For production code or large inputs, always use the iterative approach. In interviews, mention both but implement iterative unless recursion is specifically requested.
Watch prev, curr, and next rewire your list
Paste your linked list into LearnBug and see every pointer flip and advance step by step.