Reverse Linked List II
Keep prev on the node just before the range, and cur on the first node of the range. Each step takes the node right after cur and puts it right after prev, at the front of the range. cur never moves, so the range fills up backwards. After right − left steps the range is reversed, and both ends have been stitched by the same moves.
The range is reversed once right − left moves are done. Each move puts one more node at the front of the range, so the range reads backwards, and cur sits just before the node at position right + 1.
Problem
You get the head of a singly linked list, plus two positions, left and right. Positions start at 1.
Reverse only the nodes from position left to position right. The nodes before left and after right stay where they are. Return the head of the new list.
head, the first node of the list, and the two positions left and right (with left ≤ right).
The head of the list after the range from left to right is reversed.
Positions 2 to 4 hold 2, 3, 4. Reversed, they read 4, 3, 2. Nodes 1 and 5 stay where they are.
The range is the only node. Reversing one node changes nothing.
- • The list has between 1 and 500 nodes.
- • -500 ≤ Node.val ≤ 500
- • 1 ≤ left ≤ right ≤ n, where n is the number of nodes.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
If I reverse positions 2 to 4 of [1, 2, 3, 4, 5], which nodes change place, and which ones do not?
- 2.
Which node do I need to hold on to, so I can attach the reversed range back to the rest of the list?
- 3.
What is the node before position 1? Does a list always have one, and what does that mean for my code?
- 4.
Can I reverse the range by moving nodes, instead of copying values? Which node has to move each time?
- 5.
If the range has k nodes and its first node stays put, how many nodes actually need to move?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Store the nodes in a vector, reverse positions left..right, relink | O(N) | O(N) |
| Optimal | Dummy node, walk to position left − 1, move right − left nodes to the front of the range | O(N) | O(1) |
Why does this belong to this pattern?
- “reverse positions left to right”Only a middle range flips → find the node before the range and keep it as the anchor.
- “the rest of the list stays”Stitch both ends: prev points at the new front, and the old first node points at the node after the range.
- “singly linked list”We can't walk back to the node before the range, so keep prev and move the range's nodes to the front one at a time.
This is Reverse Linked List with the reversal limited to a range. The full version reverses everything, so it starts with prev = null, and the old tail becomes the new head. Here the nodes before left and after right must keep their places. So we start from a node in front of the range, and the range is attached back to the node after it. The next two questions in this sub-pattern reuse the anchor idea: Merge In Between swaps the range for another list, and Reverse Nodes in Even Length Groups repeats a sublist reversal on several groups.
Don't make this mistake
Pattern confusion: the full reversal starts with prev = null and flips every node in the list. On [1, 2, 3, 4, 5] with left = 2 and right = 4, it would give [5, 4, 3, 2, 1]. The right answer is [1, 4, 3, 2, 5]: node 1 stays before the range, and node 5 stays after it. Only the range may move, so the anchor node before it has to stay put.
The range has right − left + 1 nodes, but its first node (cur) stays in place. Only right − left nodes need to move. One extra move pulls the node after right into the range, so the list comes out wrong.
prev->next is the only link to the old front of the range. If you overwrite it first, the old front and every node behind it are lost, and nxt ends up pointing at itself, which makes a cycle.
When left = 1 the new head is a node inside the range, and nothing before it can point at the new head. The dummy gives every case a node to stitch onto, and return dummy.next returns the correct head.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.