AlgoMaster Logo

Reverse Linked List II

Last Updated: March 29, 2026

medium
3 min read

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 tricky part is stitching everything back together correctly: the node before the reversed section needs to point to the new head of the reversed portion, and the original head of the reversed portion (now the tail) needs to point to whatever comes after position right.

The key observation is that we need to keep track of four critical nodes: the node just before the reversed section, the first node of the reversed section (which becomes the tail after reversal), the last node of the reversed section (which becomes the head after reversal), and the node just after the reversed section. Getting these connections right is what makes this problem medium difficulty.

Key Constraints:

  • 1 <= n <= 500 → With n up to 500, even O(n^2) would be well within limits. But since we're working with a linked list, a single-pass O(n) solution is natural and expected.
  • -500 <= Node.val <= 500 → Node values are small integers. This doesn't affect our algorithm since we're rearranging pointers, not comparing values.
  • 1 <= left <= right <= n → Both positions are guaranteed to be valid. We don't need to handle cases where left or right are out of bounds. Also, left can equal right, meaning "reverse a single node" (no-op).

Approach 1: Extract, Reverse, and Reinsert

Intuition

The most straightforward way to think about this: collect the values from position left to right, reverse them, and write them back into the corresponding nodes. This avoids any pointer manipulation headaches by treating the linked list more like an array.

We traverse the list twice. First, we find the node at position left and collect values up to position right. Then we reverse those values and write them back starting from position left. The list structure stays the same; only the values 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 works correctly but uses extra space and modifies values instead of structure. What if we reversed the sublist by manipulating next pointers directly, doing everything in a single pass with O(1) space?

Approach 2: In-Place Reversal with Pointer Manipulation

Intuition

Instead of extracting values, we can reverse the sublist by rewiring pointers. The idea is the classic linked list reversal technique (using prev, curr, next pointers), but applied only to the nodes between positions left and right.

Here's the plan: first, traverse to the node just before position left. Then reverse the next right - left links. Finally, reconnect the reversed section with the rest of the list.

A dummy node before the head makes the code cleaner. Without it, the case where left = 1 (reversing from the very beginning) requires special handling because there's no "node before the start." The dummy node gives us a consistent previous node to work with in all cases.

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