The Three-Pointer Strategy
Reversing a singly linked list is the most common linked list interview question. The naive instinct — "just swap the values" — fails because you need to reverse the direction of traversal, not just the data. You must flip every next pointer to point backward.
The challenge: when you flip curr.next to point backward, you lose your reference to the rest of the list. The solution is to save the next node before flipping.
You maintain three variables:
prev: the node thatcurrshould now point to (starts asnull)curr: the node currently being processed (starts athead)next_temp: a temporary save ofcurr.nextbefore we overwrite it
Each iteration performs four steps:
Step 1: next_temp = curr.next # Save the rest of the list
Step 2: curr.next = prev # Flip the pointer backward
Step 3: prev = curr # Advance prev
Step 4: curr = next_temp # Advance curr
Tracing through 1 → 2 → 3 → null:
Initial: prev=null, curr=1
Iter 1: next_temp=2, 1.next=null, prev=1, curr=2
List so far: null ← 1 2 → 3 → null
Iter 2: next_temp=3, 2.next=1, prev=2, curr=3
List so far: null ← 1 ← 2 3 → null
Iter 3: next_temp=null, 3.next=2, prev=3, curr=null
List so far: null ← 1 ← 2 ← 3
Loop ends (curr is null). Return prev (node 3) as new Head.
Result: 3 → 2 → 1 → null ✓
Key Point: The termination condition is curr is null, not curr.next is null. Stopping one step early would leave the last node unprocessed. This off-by-one error is the most common mistake in reversal implementations and is specifically tested by interviewers who ask you to trace through the algorithm manually.