Learn the pattern · ~16 min
In-place Reversal
also called pointer rewiring, dummy head
Walk the list once with three pointers, and at each node save the next node, then turn its arrow backwards.
Reach for it when you read
- +Reverse a linked list, or a piece of it between two positions, in place.
- +Reverse nodes in groups of k (pairs are k = 2).
- +Compare or weave a list's first half with its second half backwards: palindrome list, reorder list.
- +The task says O(1) extra memory and the answer is the same nodes in a new order.
Not this one when
- ×Reversing an array or a string: swap from both ends, no pointer surgery.
- ×Reading a list backwards just to print or sum it: a stack or recursion is enough if memory is allowed.
Learn In-place Reversal
First, what does reversing a list mean?
In a linked list each node holds a value and one arrow, next, to the next node. 1 → 2 → 3 is three nodes: 1 points at 2, 2 points at 3, and 3 points at null.
Reversing it gives 3 → 2 → 1. The nodes don't move and the values don't change. Only the arrows turn around: 3 now points at 2, 2 at 1, and 1 at null. The list's head is now 3.
How do we turn the arrows?
Take 1 → 2 → 3 → 4 → 5. Walk it once with three pointers: prev (the part already reversed), curr (the node we're turning) and nxt (the rest). Start with prev = null, curr = 1. Each step:
Save
curr->nextinnxt, pointcurrback atprev, then moveprevandcurrone node forward.
| step | what happens | reversed part (prev) |
still to do (curr) |
|---|---|---|---|
| start | nothing turned yet | null | 1 → 2 → 3 → 4 → 5 |
| 1 | save 2, point 1 at null | 1 | 2 → 3 → 4 → 5 |
| 2 | save 3, point 2 at 1 | 2 → 1 | 3 → 4 → 5 |
| 3 | save 4, point 3 at 2 | 3 → 2 → 1 | 4 → 5 |
| 4 | save 5, point 4 at 3 | 4 → 3 → 2 → 1 | 5 |
| 5 | save null, point 5 at 4 | 5 → 4 → 3 → 2 → 1 | null |
Each step moves one node from the right column to the front of the middle one. When curr is null, prev is the reversed list.
The code for it
ListNode *prev = nullptr, *curr = head;
while (curr) {
ListNode* nxt = curr->next; // save the rest first
curr->next = prev; // turn the arrow
prev = curr;
curr = nxt;
}
// prev is the new headYou don't need to memorise this. It's the table: save, turn, step forward.
Why save nxt first? At step 2, the only arrow leading to 3 is 2->next. Once we write 2->next = 1, nothing in the program knows where 3, 4 and 5 are.
Fine, but how does this help us?
Here's a classic: Reverse Linked List II. Reverse only positions left to right (counting from 1), in place. Take 1 → 2 → 3 → 4 → 5 with left = 2, right = 4.
By hand: the piece 2, 3, 4 becomes 4, 3, 2, and the nodes around it stay. Which arrows change?
| node | arrow before | arrow after | why |
|---|---|---|---|
| 1 | 2 | 4 | before the piece: must lead into its new start |
| 2 | 3 | 5 | the piece's new last node: must lead out of it |
| 3 | 4 | 2 | inside the piece: turned around |
| 4 | 5 | 3 | inside the piece: turned around |
| 5 | null | null | after the piece: untouched |
So the answer is 1 → 4 → 3 → 2 → 5.
The brute force
Copy the values into an array, reverse the slice, write them back:
ListNode* reverseBetweenCopy(ListNode* head, int left, int right) {
vector<int> vals;
for (ListNode* p = head; p; p = p->next) vals.push_back(p->val);
reverse(vals.begin() + left - 1, vals.begin() + right);
int i = 0;
for (ListNode* p = head; p; p = p->next) p->val = vals[i++];
return head;
}To be honest, LeetCode accepts this: the list has at most 500 nodes. But it takes O(n) extra memory and two passes (the follow-up asks for one), and it moves values, not nodes, while a real node can carry far more than one int. Keep O(1) memory by swapping values pair by pair, walking from the head each time, and a 10⁵-node piece costs about 5 × 10⁹ steps: 4 s measured.
The one thought that fixes it
Walk a pointer before to node 1, the node just before the piece. Then run the reversal loop from above on the piece, exactly right − left + 1 = 3 times, starting at node 2.
Stop right after those 3 turns. prev is 4 → 3 → 2 → null, and curr is 5. Compare it with the hand table:
- 3 and 4 already point the right way: the loop turned them.
- 2 points at
null, but it should point at 5. And 5 iscurr. - 1 still points at 2, but it should point at 4. And 4 is
prev.
So the loop leaves two loose ends, and both nodes we need are in hand. Node 2 is still before->next, since 1's arrow hasn't changed. Two lines fix it, in this order:
before->next->next = curr: 2 → 5.before->next = prev: 1 → 4.
Swap the two lines and the first one overwrites 1's arrow, the only way back to 2.
If left = 1, no node comes before the piece. Put a dummy node in front of the head and before always exists.
ListNode* reverseBetween(ListNode* head, int left, int right) {
ListNode dummy(0, head); // so there's always a node before the piece
ListNode* before = &dummy;
for (int i = 1; i < left; i++) before = before->next;
ListNode* prev = nullptr;
ListNode* curr = before->next;
for (int i = 0; i <= right - left; i++) { // the same loop, right - left + 1 times
ListNode* nxt = curr->next;
curr->next = prev;
prev = curr;
curr = nxt;
}
before->next->next = curr; // old start (2) → node after the piece (5)
before->next = prev; // node before (1) → new start (4)
return dummy.next;
}Dry run
Same list, left = 2, right = 4.
| step | what happens | prev chain |
curr |
|---|---|---|---|
| walk | before moves from the dummy to node 1 |
null | 2 |
| turn 1 | save 3, point 2 at null | 2 | 3 |
| turn 2 | save 4, point 3 at 2 | 3 → 2 | 4 |
| turn 3 | save 5, point 4 at 3 | 4 → 3 → 2 | 5 |
| stitch | 2 → 5, then 1 → 4 |
The list reads 1 → 4 → 3 → 2 → 5, the same answer as the brute force.
Why it's O(n)
Count what happens to each node. before walks past at most n of them, each node in the piece has its arrow turned once, and the stitch changes two more: O(n) time. A dummy and four pointers: O(1) space.
From O(n) extra memory (or O(n²) time) down to O(1) memory in one pass, by turning arrows instead of copying values.
Groups of k? Count first, then the same loop
Reverse Nodes in k-Group reverses every block of k nodes and leaves a short last block alone. Repeat the piece reversal: check that k nodes are left, turn them, stitch, move before to the group's new tail.
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode dummy(0, head);
ListNode* before = &dummy;
while (true) {
ListNode* check = before;
for (int i = 0; i < k && check; i++) check = check->next;
if (!check) break; // fewer than k left: keep them
ListNode *prev = nullptr, *curr = before->next;
for (int i = 0; i < k; i++) {
ListNode* nxt = curr->next;
curr->next = prev;
prev = curr;
curr = nxt;
}
ListNode* tail = before->next; // old start, now the group's tail
tail->next = curr;
before->next = prev;
before = tail; // the next group starts after it
}
return dummy.next;
}On 1 → 2 → 3 → 4 → 5: k = 2 gives 2 → 1 → 4 → 3 → 5 (that's Swap Nodes in Pairs), and k = 3 gives 3 → 2 → 1 → 4 → 5.
Compare a list with itself backwards? Reverse the second half
Palindrome Linked List: does the list read the same both ways? Find the middle with fast and slow pointers, reverse from it on, then walk both halves together. For 1 → 2 → 2 → 1, the middle is the second 2, and reversing from it gives 1 → 2. Compare 1 with 1, 2 with 2: a palindrome, with O(1) memory.
Reorder List does the same split, then weaves instead of comparing: 1 → 2 → 3 → 4 → 5 becomes 1 → 5 → 2 → 4 → 3.
Remember this
- Save
nxtbefore you touchcurr->next, or the rest of the list is lost. - After the loop,
previs the new head of the reversed part andcurris the node after it. - Reversing a piece leaves two loose ends: old start →
curr, then node before →prev, in that order. A dummy head makesleft = 1no special case. - Whenever a problem asks for the same nodes in reverse order (the whole list, a piece, groups of k, or half of it) with O(1) memory, it's this loop.
Practise it on Reverse Linked List, then Reverse Linked List II (a piece), Swap Nodes in Pairs (groups of 2), Palindrome Linked List (reverse half, compare), Reorder List (reverse half, weave) and Reverse Nodes in k-Group.
Below, watch Reverse Linked List run line by line.
Watch it work, line by line
Reverse Linked List ↗, 1 → 2 → 3 → 4 → 5 → 5 → 4 → 3 → 2 → 1. Step with → and ←. The data is on the left, the code and the variables that changed on the right, and 3 times it stops to ask you what happens next.
When to use it, and when not
Ask yourself
Do I need to reverse or rewire part of a linked list in place, where copying to an array is either banned (O(1) space) or can't splice the piece back?
Reach for it when
- +reverse all or part of a list
- +reorder, rotate, swap pairs
- +compare a list with its reverse
- +O(1) extra space
It fails 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
Why this pattern and not another
You must rewire a singly linked list in place with O(1) extra space — flipping pointers, not values. The prev/curr/next loop is the only tool that re-links nodes without copying.
Recall card
Answer each one out loud before you open it. Come back in three days and try again without reading the page.
1What are the four lines inside the reversal loop, in order?Reveal
nxt = curr->next; curr->next = prev; prev = curr; curr = nxt;Save, flip, move prev, move curr.2When the loop ends, which pointer is the new head, and why?Reveal
prev.currhas run off to null, andprevheads the reversed part, which is now the whole list.3Why use a dummy head when reversing a sublist?Reveal
If the piece starts at the real head, the head changes. A dummy node before it means there's always a "node before the piece" to stitch, with no special case.
Practice ladder
Finished In-place Reversal? Mark it done and Lessgo moves you to the next step.