Linked List Manipulation
01 / The one-sentence essence
To rewrite a linked list in place, walk it with three pointers prev, curr, next — save before you flip, advance after you flip, and the rest of the list stays reachable through every step.
Problemreverse a singly linked list in placeInput[1 → 2 → 3 → 4 → 5]
Three pointers: prev at null, curr at the head. next will hold the saved pointer at each step.
step
0 / 16
prev
null
curr
1 [0]
action
init
0 / 16
02 / The pattern signature
# reverse a singly linked listprev ← nullcurr ← headwhile curr ≠ null:next ← curr.next // 1. savecurr.next ← prev // 2. flipprev ← curr // 3. advance prevcurr ← next // 4. advance currreturn prev // new head
03 / When to recognize this pattern
"in place / O(1) space"
The constraint forbids building a second list — you must rewire the existing nodes. That's where the multi-pointer dance earns its keep.
"reverse / reorder"
You're changing the structure of the list, not the values. The values stay; the
.next arrows shift."sublist / range"
Reverse a chunk between two positions, not the whole list. Same algorithm, plus stitching at the boundaries.
"merge / interleave"
Two lists into one, alternating or sorted. A "dummy head" trick simplifies the edge-of-list bookkeeping — you splice from
dummy.next at the end.04 / Common pitfalls
i.
Losing the rest of the list before saving next.
If you flip
curr.next ← prev before saving the original curr.next, you've cut the tail loose. The save step is non-negotiable — it's the whole reason there are three pointers.ii.
Wrong return value.
After the loop ends,
curr is null and prev is the new head. Returning curr would give null on a non-empty list — a common slip.iii.
Not handling the empty list.
The loop doesn't execute on
head = null, and prev stays null. That's correct — but only because the loop body never runs. Verify the empty case explicitly; it's easy to introduce a null-dereference if you peek curr.next before the guard.05 / Go practice — on LeetCode
Four problems. The first two are reversal in different shapes. The third is the merge classic. The fourth is where pointer juggling becomes load-bearing.