Algorithms / Linked List Reversal
Least You Need to Know: Reversing a Linked List with Prev, Current, and Next
Linked-list reversal is the interview archetype for pointer discipline: keep track of what was before, what you are on, and what comes next. The core invariant is that one prefix has already been reversed while the remaining suffix is still untouched.
The least you need to know
- Iterative reversal commonly uses three pointers: `prev`, `current`, and `next_node`.
- At each step, save the next node before reversing the current pointer.
- The new head after a full reversal is the old tail, which ends up in `prev`.
- Loop invariants help: one part is reversed, the rest is unreversed.
- Partial reversals need careful tracking of the nodes just before and after the reversed window.
Key notation
prev
head of the already reversed prefix
current
node currently being processed
next_node
saved successor before redirecting current.next
Tiny worked example
- Start with `prev = null`, `current = head`.
- Save `next_node = current.next`.
- Set `current.next = prev`, then move `prev = current` and `current = next_node`.
- When `current` becomes null, `prev` is the new head of the reversed list.
Common mistakes
- Students sometimes move `current` before saving the old next pointer.
- Students sometimes forget that the original head becomes the new tail and should point to null in a full reversal.
- Partial reversals require reconnecting both sides of the reversed segment.
How to recognize this kind of problem
- The task asks to reverse a whole list or a contiguous part of it.
- A prev/current/next picture makes the intended invariant obvious.
- Only pointer changes are needed; no array copy is necessary.