AlgoMaster Logo

Reverse Linked List II

mediumFrequency5 min readUpdated June 23, 2026

Understanding the Problem

This is a targeted reversal problem. Instead of reversing the entire linked list, we only reverse a sublist from position left to position right, leaving the rest untouched. The difficulty is stitching everything back together: the node before the reversed section must point to the new head of the reversed portion, and the original head of the reversed portion (now the tail) must point to whatever comes after position right.

Four nodes drive the bookkeeping: the node just before the reversed section, the first node of the section (which becomes its tail after reversal), the last node of the section (which becomes its head), and the node just after the section. Wiring these four connections correctly is the whole problem.

Key Constraints:

  • 1 <= n <= 500 → The list is small, so the time bound is not the binding constraint here. The follow-up asks for a single pass, which an O(n) pointer-rewiring solution satisfies.
  • 1 <= left <= right <= n → Both positions are valid, so there is no need to guard against out-of-bounds positions. When left equals right, the range is a single node and the list is unchanged.
  • -500 <= Node.val <= 500 → Values are not compared or summed, so their range does not affect the algorithm.

Approach 1: Extract, Reverse, and Reinsert

Intuition

Collect the values from position left to right, reverse that list of values, and write them back into the same nodes. This treats the linked list like an array and sidesteps pointer rewiring entirely.

We traverse the list twice. The first pass collects the values from position left to right. The second pass walks back to position left and overwrites each node's value with the corresponding value from the reversed list. The node structure never changes; only the values stored inside the nodes change.

Algorithm

  1. Traverse to the node at position left, collecting all values from position left to right into a list.
  2. Reverse the collected values.
  3. Traverse to position left again and overwrite each node's value with the corresponding reversed value.
  4. Return the head.

Example Walkthrough

1Initial: head = [1, 2, 3, 4, 5], left=2, right=4
1
2
left
3
4
right
5
null
1/5

Code

This approach is correct, but it uses O(n) extra space and rewrites node values rather than reorganizing the list. The next approach reverses the sublist by rewiring next pointers directly, in a single pass with O(1) space.

Approach 2: In-Place Reversal with Pointer Manipulation

Intuition

Instead of extracting values, reverse the sublist by rewiring pointers. The technique is head insertion: keep curr fixed on the first node of the range, and repeatedly pull the node directly after curr out and splice it to the front of the reversed section. After right - left such moves, the section is reversed and the surrounding links are already correct.

A dummy node before the head removes the special case where left = 1. When the reversal starts at the very first node, there is no real "node before the start" to attach the reversed section to. Pointing the dummy at head gives a single node, prev, that sits before the range in every case, so the same loop handles left = 1 and left > 1 without a branch.

Algorithm

  1. Create a dummy node that points to head. Set prev = dummy.
  2. Move prev forward left - 1 times so it points to the node just before position left.
  3. Set curr = prev.next (the first node to be reversed).
  4. For i from 0 to right - left - 1:
    • Save nextNode = curr.next.
    • Set curr.next = nextNode.next (skip over nextNode).
    • Set nextNode.next = prev.next (place nextNode at the front of the reversed section).
    • Set prev.next = nextNode (update the entry point to the reversed section).
  5. Return dummy.next.

Example Walkthrough

1Initial: prev at node before pos 2, curr at pos 2
prev
1
2
curr
3
4
5
null
1/6

Code