Lesson 8.3 · Linked Lists
Reversing a List In Place
Walk with three pointers (prev, curr, next) and turn each arrow around. The building block for many harder list problems.
12 min
Think of it like this
Turning a conga line around: each dancer, one at a time, lets go of the person ahead and grabs the person behind. You must remember who was ahead before letting go, or the rest of the line wanders off.
1.Three pointers
Save next = curr.next (so the rest isn't lost), point curr.next = prev (turn the arrow), then move prev = curr and curr = next. When curr is null, prev is the new head.
head = 1 → 2 → 3 → nullState(vars)
Step 1/4Start: prev = null, curr = 1.
2.Recursive version
Reverse the rest of the list recursively, then make the next node point back: head.next.next = head; head.next = null;. It's elegant, but uses O(n) stack space and can overflow on very long lists. The iterative version is preferred.
Remember
- Save next → reverse arrow → advance prev and curr.
- prev is the new head at the end.
- Iterative is O(1) space; recursive is O(n) stack.
Common mistakes
- Forgetting to save
curr.nextbefore overwriting it. - Returning
curr(null) instead ofprev.