Reverse Linked List II: Reverse a Sublist in One Pass
Reverse Linked List II is LeetCode problem 92. You are given the head of a singly linked list and two integers left and right (1-indexed). Reverse the nodes from position left to position right, and return the modified list.
Example: [1, 2, 3, 4, 5] with left = 2, right = 4 produces [1, 4, 3, 2, 5].
This is a step up from Reverse Linked List (LeetCode 206). There, you reverse the entire list. Here, you reverse only a sublist. The reversal logic is the same, but you also need to stitch the reversed portion back into the surrounding list without breaking anything.
The approach
You can solve this in a single pass with three key ideas:
- Dummy head. Create a dummy node that points to
head. This handles the edge case whereleft = 1(there is no node before the sublist) without special-casing it. - Find the node before left. Walk a pointer
prevforwardleft - 1times so it lands on the node just before positionleft. - Reverse the sublist by moving nodes to the front. Instead of the classic prev/curr/next three-pointer reversal, you keep
currfixed at positionleftand repeatedly take the node aftercurr(call itthen) and move it to the front of the reversed section. Afterright - leftiterations, the sublist is reversed.
The "move to front" technique avoids tracking multiple shifting pointers. prev never moves. curr never moves. Only then advances, and each iteration inserts it right after prev.
Visual walkthrough
Trace through [1, 2, 3, 4, 5] with left = 2, right = 4. Watch how prev (purple) stays at node 1, curr (amber) stays at node 2, and each iteration pulls the next node to the front of the reversed section.
Setup: Add a dummy node. Walk prev to the node before position left.
prev points to node 1 (position left-1). curr is node 2 (position left). then is curr.next (node 3).
Iteration 1: Move node 3 to the front of the reversed section.
curr.next = then.next (2 points to 4). then.next = prev.next (3 points to 2). prev.next = then (1 points to 3). Then update then = curr.next.
Iteration 2: Move node 4 to the front of the reversed section.
curr.next = then.next (2 points to 5). then.next = prev.next (4 points to 3). prev.next = then (1 points to 4). Sublist is now fully reversed.
Done: Return dummy.next. The sublist from position 2 to 4 is reversed.
Final list: 1 -> 4 -> 3 -> 2 -> 5 -> null. Positions 2 through 4 are reversed. Everything else is unchanged.
Notice that prev and curr never move. Each iteration does three pointer reassignments, and after two iterations (since right - left = 2), the sublist is fully reversed.
Python solution
def reverseBetween(head, left, right):
dummy = ListNode(0, head)
prev = dummy
for _ in range(left - 1):
prev = prev.next
curr = prev.next
for _ in range(right - left):
then = curr.next
curr.next = then.next
then.next = prev.next
prev.next = then
return dummy.next
The inner loop runs exactly right - left times. Each pass moves one node from after curr to the front of the reversed section. The order of the three assignments matters: disconnect then, insert then after prev, then update prev.next.
Step-by-step explanation
Here is what happens on the example [1, 2, 3, 4, 5], left = 2, right = 4:
- Create dummy node.
dummy -> 1 -> 2 -> 3 -> 4 -> 5. - Advance prev. Move
prevforwardleft - 1 = 1step. Nowprev = node 1. - Set curr.
curr = prev.next = node 2. - Iteration 1 (
then = node 3):curr.next = then.next(node 2 now points to node 4)then.next = prev.next(node 3 now points to node 2)prev.next = then(node 1 now points to node 3)- List is now
1 -> 3 -> 2 -> 4 -> 5.
- Iteration 2 (
then = node 4):curr.next = then.next(node 2 now points to node 5)then.next = prev.next(node 4 now points to node 3)prev.next = then(node 1 now points to node 4)- List is now
1 -> 4 -> 3 -> 2 -> 5.
- Return dummy.next. The answer is
[1, 4, 3, 2, 5].
Complexity analysis
Time: O(n). You traverse the list once to find the node before left, then perform right - left swaps. In the worst case (left = 1, right = n), you touch every node exactly once.
Space: O(1). You only use a fixed number of pointer variables (dummy, prev, curr, then). No recursion, no extra data structures.
The building blocks
This problem is built from two core building blocks:
Partial in-place reversal. You already know how to reverse an entire linked list. This problem asks you to reverse only a segment. The trick is the "move to front" variant: instead of marching two pointers through the list, you keep one pointer fixed at the start of the sublist and repeatedly pluck the next node forward. This avoids the complexity of reconnecting boundaries after the fact.
Dummy head for edge cases. When left = 1, there is no node before the reversed section. A dummy node that points to head gives you a consistent prev pointer regardless of where the sublist starts. Without it, you would need a separate code path for left = 1.
These two building blocks combine in more advanced problems:
- Reverse Linked List (LeetCode 206) is the simpler base case where the entire list is reversed.
- Reverse Nodes in k-Group (LeetCode 25) applies the same sublist reversal repeatedly in groups of k.
Edge cases
left = 1. The dummy node handles this.prevstays atdummy, and the reversal proceeds normally.left = right. The inner loop runs zero times. No reversal happens. The list is returned unchanged.- Reverse the entire list (
left = 1,right = n). This degenerates to a full reversal. The algorithm still works correctly because the loop runsn - 1times. - Two-node list. With
left = 1,right = 2, the loop runs once and swaps the two nodes. Good for verifying your logic by hand.
From understanding to recall
This is a ten-line solution with three pointer reassignments inside a loop. You will read it, understand the logic, and feel confident. Then in an interview you will mix up the order of curr.next = then.next and then.next = prev.next and spend five minutes untangling a broken list.
The fix is not more studying. It is more reps. You need to type this solution from scratch enough times that the three-line dance inside the loop is automatic: disconnect then, insert then at front, update prev.next. Spaced repetition locks this into muscle memory so you can reproduce it under pressure without hesitation.
Related posts
- Reverse Linked List - The simpler base case where you reverse the entire list using the three-pointer technique
- Reverse Nodes in k-Group - Applies the same sublist reversal repeatedly in groups of k, the hard version of this pattern