In-place Reversal
O(n) / O(1)
Re-point next pointers one node at a time while carrying prev/curr; for k-groups, reverse a chunk then stitch.
0/11 solved
Recognise it
The signal
Reverse a whole list, a sublist [left, right], or every k nodes; reorder, rotate, swap pairs, palindrome check, in O(1) space.
1Brute force
Copy values to an array, manipulate, write back.
2The observation
O(n) extra space and copying; pointer surgery does it in place.
3Why it works
prevheads the already-reversed part,currheads the untouched part; savenextbefore re-pointing so nothing is dropped.
Reach for it when
- +Reverse a whole list, a sublist [left,right], or every k nodes — in O(1) space.
- +Rearrange a list by reversing part of it — reorder list, palindrome check, swap pairs.
- +Any pointer-rewiring where recursion's O(n) stack is unacceptable.
Not when
- ×You need random access or to sort by valueuse Copy to an array
- ×You only need to read values in reverse, not rewire, and O(n) space is fineuse A stack of values
- ×It's really cycle detection or finding the middle (often a step before reversing)use Fast & slow pointers
- ×The list is immutable or shared with other codeuse Don't rewire it; build a copy
- ×You think 'dump to an array and reverse': O(n) space and can't splice a segment back inuse Rewire
.nextin place - ×You think opposite-end swapping works: it needs O(1) indexing, which lists lackuse Iterative prev/curr/next
Intuition
Reversing a linked list means flipping every next pointer to point backward, using O(1) extra space (no array copy). The trick is a three-pointer dance: prev, curr, and a saved next.
Read the full intuition
The loop, every iteration:
- Save
nxt = curr.next(or you lose the rest of the list). - Flip
curr.next = prev(point this node backward). - Slide
prev = curr, thencurr = nxt.
When curr falls off the end, prev is the new head. Three lines of pointer-shuffling that you should be able to write in your sleep — it's the building block for reversing a sublist, swapping pairs, reversing in k-groups, and rearranging lists (reorder, palindrome).
Key idea
The hard variants all reduce to "find the boundary nodes, reverse the middle, then re-stitch the ends." Use a dummy head node so the case where the reversal includes the real head stops being a special case.
Why it costs what it costs
Time O(n). each node's pointer is rewired exactly once.
Space O(1). iterative version uses three pointers; recursion (k-group) adds O(n/k) or O(n) stack.
Practice
- Reverse Linked ListFoundationFoundationAmazon, MetaEasy
- Reverse Linked List IIApply it · Reverse only positions left..right.Apply itMedium
- Swap Nodes in PairsOne change · Reverse every group of 2.One changeAmazon, AdobeMedium
- Reverse Nodes in k-GroupTwists · Groups of k; the last incomplete group stays as is.TwistsAmazon, MicrosoftHard
- Rotate ListBranches · Rotation instead of reversal.BranchesAmazon, AdobeMedium
- Reorder ListCombine · Reversal is one step in a pipeline.CombineAmazon, MetaMedium
- Odd Even Linked ListAmazon, AdobeMedium
- Remove Duplicates from Sorted ListAmazon, Morgan StanleyEasy
- Add Two NumbersAmazon, AdobeMedium
- Copy List with Random PointerAmazon, MetaMedium
- Palindrome Linked ListAmazon, AdobeEasy
Pitfalls
Classic bugs
- !Lost the rest of the list: save
nxt = curr.nextbefore reassigningcurr.next. - !No dummy head, so reversing a range that includes the real head becomes a buggy special case.
- !k-group: reverse a group only if k full nodes remain; leave the trailing partial group as-is.
- !After reversing a middle segment, re-stitch both ends: node before → new sub-head, old sub-head → node after.
- !Recursive reversal uses O(n) stack and can overflow on long lists; use the iterative form.
Go deepervariants and techniques · 1 combinations · interviewer follow-ups · resources
Variants and techniques
- Full reverseprev/curr/next loop; return prev. Reverse Linked List.
- Sublist reverseDummy + 'before' pointer, hoist nodes to the front of the segment. Reverse Linked List II.
- k-group reverseReverse each full block of k, recurse/iterate the rest, stitch. Reverse Nodes in k-Group, Swap Nodes in Pairs (k=2).
- Reverse half + interleaveFind middle (fast/slow), reverse second half, merge. Reorder List, Palindrome Linked List.
- Rotate / splitFind length, reconnect into a ring, cut at the new head. Rotate List.
Combinations
Fast & Slow Pointers+In-place Reversal
palindrome list, reorder list
- Fast & Slow Pointers:
- finds the middle
- In-place Reversal:
- reverses the second half
Follow-ups interviewers ask
- 01Reverse in groups of k instead of the whole list? → reverse each full block, recurse/iterate on the rest, stitch boundaries.
- 02What if it were a doubly linked list? → also fix
prevpointers; a single swap of next/prev per node reverses it. - 03Can you do it recursively, and what's the space cost? → yes, but O(n) stack vs O(1) iterative — discuss the tradeoff.
- 04What if the final partial group should also be reversed? → drop the 'k full nodes' guard and reverse whatever remains.
State table
| step | prev | curr | saved next | structure |
|---|---|---|---|---|
| init | None |
1 |
— | 1→2→3 |
| 1 | 1 |
2 |
2 |
1→None, rest 2→3 |
| 2 | 2 |
3 |
3 |
2→1→None |
| 3 | 3 |
None |
— | 3→2→1→None ✓ |