Reverse Linked List II (LeetCode 92): Solution and Explanation

Medium
Linked List

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)

  1. Create dummy with dummy.next = head and set prev = dummy.
  2. Move prev forward left - 1 times so it sits just before position left.
  3. Set curr = prev.next, the first node of the range.
  4. Repeat right - left times: nxt = curr.next, then curr.next = nxt.next to unhook it, nxt.next = prev.next to point it at the current front, and prev.next = nxt to land it at the front.
  5. Return dummy.next.

Step-by-step example

head = [1,2,3,4,5], left = 2, right = 4
  1. prev walks one step from the dummy to 1. curr is 2, the first node of the range. There are 4 - 2 = 2 moves to make.
  2. Move 1: nxt is 3. 2 now points at 4, 3 points at 2, and 1 points at 3. The list reads 1, 3, 2, 4, 5.
  3. Move 2: nxt is 4, the node after curr (still 2). 2 now points at 5, 4 points at 3, and 1 points at 4. The list reads 1, 4, 3, 2, 5.
  4. The loop ends. curr (2) is at the back of the range and already points at 5, so returning dummy.next gives [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.next
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

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.

Space

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 = 1 the head itself changes, and without a dummy that needs a separate branch.
  • Moving curr inside the loop. curr must 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.next must come before nxt.next is overwritten, or the rest of the list is lost.
  • Running the loop right - left + 1 times. 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.