Reverse Linked List II (LeetCode 92): Solution and Explanation
Given the head of a singly linked list and positions left and right, reverse only the nodes from position left to position right and return the head of the list.
Intuition
Reversing a whole list flips every pointer. Here only a middle stretch flips, and the hard part is reconnecting both ends of that stretch to the untouched parts of the list.
There is a way to reverse a range that keeps it connected the whole time. Take the range 2, 3, 4. Move 3 to the front of the range to get 3, 2, 4, then move 4 to the front to get 4, 3, 2. Every move takes the node right after the original first node and puts it at the front.
Two pointers make this cheap. prev sits on the node just before the range and never moves, so the front of the range is always prev.next. curr stays on the original first node, which drifts back one place per move, so the node to move next is always curr.next.
Each move is three pointer assignments, and because curr keeps pointing at whatever follows the range, the tail stays attached without any clean-up at the end. A dummy node in front of the head lets left = 1 use exactly the same code.
Approach: Move-to-front inside the range (one pass)
- Create
dummywithdummy.next = headand setprev = dummy. - Move
prevforwardleft - 1times so it sits just before positionleft. - Set
curr = prev.next, the first node of the range. - Repeat
right - lefttimes:nxt = curr.next, thencurr.next = nxt.nextto unhook it,nxt.next = prev.nextto point it at the current front, andprev.next = nxtto land it at the front. - Return
dummy.next.
Step-by-step example
head = [1,2,3,4,5], left = 2, right = 4
prevwalks one step from the dummy to 1.curris 2, the first node of the range. There are 4 - 2 = 2 moves to make.- Move 1:
nxtis 3. 2 now points at 4, 3 points at 2, and 1 points at 3. The list reads 1, 3, 2, 4, 5. - Move 2:
nxtis 4, the node aftercurr(still 2). 2 now points at 5, 4 points at 3, and 1 points at 4. The list reads 1, 4, 3, 2, 5. - The loop ends.
curr(2) is at the back of the range and already points at 5, so returningdummy.nextgives [1,4,3,2,5].
Code: Python, Java, C++
1class Solution:
2 def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
3 dummy = ListNode(0, head)
4 prev = dummy
5 for _ in range(left - 1):
6 prev = prev.next
7 curr = prev.next
8 for _ in range(right - left):
9 nxt = curr.next
10 curr.next = nxt.next
11 nxt.next = prev.next
12 prev.next = nxt
13 return dummy.next1class Solution {
2 public ListNode reverseBetween(ListNode head, int left, int right) {
3 ListNode dummy = new ListNode(0, head);
4 ListNode prev = dummy;
5 for (int i = 0; i < left - 1; i++) {
6 prev = prev.next;
7 }
8 ListNode curr = prev.next;
9 for (int i = 0; i < right - left; i++) {
10 ListNode nxt = curr.next;
11 curr.next = nxt.next;
12 nxt.next = prev.next;
13 prev.next = nxt;
14 }
15 return dummy.next;
16 }
17}1class Solution {
2public:
3 ListNode* reverseBetween(ListNode* head, int left, int right) {
4 ListNode dummy(0, head);
5 ListNode* prev = &dummy;
6 for (int i = 0; i < left - 1; i++) {
7 prev = prev->next;
8 }
9 ListNode* curr = prev->next;
10 for (int i = 0; i < right - left; i++) {
11 ListNode* nxt = curr->next;
12 curr->next = nxt->next;
13 nxt->next = prev->next;
14 prev->next = nxt;
15 }
16 return dummy.next;
17 }
18};Time and space complexity
O(n)
prev walks at most left - 1 nodes and the loop makes right - left constant-time moves, so every node is touched a constant number of times.
O(1)
Only the dummy node and three pointers are added; the nodes are relinked in place.
Other approaches
Reverse the sublist, then reconnect (time O(n), space O(1))
Walk to the node before the range, reverse the right - left + 1 nodes with the classic three-pointer reversal, then attach the old first node to what follows and prev to the new first node. Same cost, but the two reconnections at the end are easy to get wrong.
Copy values into an array (time O(n), space O(n))
Read all values into an array, reverse the slice from left - 1 to right - 1, and write the values back into the nodes. Simple, but it uses O(n) extra memory and changes values instead of relinking nodes.
Recursion (time O(n), space O(n))
Recursively reverse the first right - left + 1 nodes starting at position left, remembering the successor node to reattach. Elegant, but the call stack costs O(n) space.
Common mistakes
- Skipping the dummy node. When
left = 1the head itself changes, and without a dummy that needs a separate branch. - Moving
currinside the loop.currmust stay on the original first node; the node to move is always the one right after it. - Doing the three assignments in the wrong order.
curr.next = nxt.nextmust come beforenxt.nextis overwritten, or the rest of the list is lost. - Running the loop
right - left + 1times. A range of m nodes needs only m - 1 moves; one more would pull in the node after the range.
Frequently asked questions
Why does curr not move during the loop?
curr holds the original first node of the range. Each move puts a node in front of it, so it slides back one place by itself. Keeping it fixed means curr.next is always the next node to move and curr always points at whatever follows the range.
What happens when left equals right?
The loop runs right - left = 0 times, so nothing changes and the original list is returned.
Is this really one pass?
Yes. prev reaches the range once, and the moves walk through the range once. No node is visited again after the range ends, so the whole list is read at most once.
How is this different from Reverse Linked List?
Reverse Linked List flips the whole list, so there is nothing to reconnect. Here only part of the list flips, and the move-to-front method keeps both ends connected while it works.