2Brute2Best
🔥0

Reverse Linked List II

Medium·LeetCode #92Basic Anchor
LeetCode
30-Second Mental ModelLinked List — Reversal (Sublist: find the node before the range, reverse exactly the range, then stitch both ends)
Complexity:O(N)→O(N)
Key Insight

Keep prev on the node just before the range, and cur on the first node of the range. Each step takes the node right after cur and puts it right after prev, at the front of the range. cur never moves, so the range fills up backwards. After right − left steps the range is reversed, and both ends have been stitched by the same moves.

Core Validity Invariant

The range is reversed once right − left moves are done. Each move puts one more node at the front of the range, so the range reads backwards, and cur sits just before the node at position right + 1.

01

Problem

You get the head of a singly linked list, plus two positions, left and right. Positions start at 1.

Reverse only the nodes from position left to position right. The nodes before left and after right stay where they are. Return the head of the new list.

Input

head, the first node of the list, and the two positions left and right (with left ≤ right).

Output

The head of the list after the range from left to right is reversed.

Example 1
Input: head = [1, 2, 3, 4, 5], left = 2, right = 4
Output: [1, 4, 3, 2, 5]

Positions 2 to 4 hold 2, 3, 4. Reversed, they read 4, 3, 2. Nodes 1 and 5 stay where they are.

Example 2
Input: head = [5], left = 1, right = 1
Output: [5]

The range is the only node. Reversing one node changes nothing.

Constraints
  • • The list has between 1 and 500 nodes.
  • • -500 ≤ Node.val ≤ 500
  • • 1 ≤ left ≤ right ≤ n, where n is the number of nodes.
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.

  1. 1.

    If I reverse positions 2 to 4 of [1, 2, 3, 4, 5], which nodes change place, and which ones do not?

  2. 2.

    Which node do I need to hold on to, so I can attach the reversed range back to the rest of the list?

  3. 3.

    What is the node before position 1? Does a list always have one, and what does that mean for my code?

  4. 4.

    Can I reverse the range by moving nodes, instead of copying values? Which node has to move each time?

  5. 5.

    If the range has k nodes and its first node stays put, how many nodes actually need to move?

03

Brute force approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceStore the nodes in a vector, reverse positions left..right, relinkO(N)O(N)
OptimalDummy node, walk to position left − 1, move right − left nodes to the front of the rangeO(N)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • “reverse positions left to right”Only a middle range flips → find the node before the range and keep it as the anchor.
  • “the rest of the list stays”Stitch both ends: prev points at the new front, and the old first node points at the node after the range.
  • “singly linked list”We can't walk back to the node before the range, so keep prev and move the range's nodes to the front one at a time.
node before the range (prev)+move right − left nodes to the front of the range+stitch both ends→Linked List — Reversal (Sublist: find the node before the range, reverse exactly the range, then stitch both ends)

This is Reverse Linked List with the reversal limited to a range. The full version reverses everything, so it starts with prev = null, and the old tail becomes the new head. Here the nodes before left and after right must keep their places. So we start from a node in front of the range, and the range is attached back to the node after it. The next two questions in this sub-pattern reuse the anchor idea: Merge In Between swaps the range for another list, and Reverse Nodes in Even Length Groups repeats a sublist reversal on several groups.

10

Don't make this mistake

Reversing the whole list, like Reverse Linked List
✗ ListNode* prev = nullptr; for (ListNode* cur = head; cur != nullptr; ) { ... } // flips every node
✓ Walk to the node before `left`, then flip only `right − left + 1` nodes.

Pattern confusion: the full reversal starts with prev = null and flips every node in the list. On [1, 2, 3, 4, 5] with left = 2 and right = 4, it would give [5, 4, 3, 2, 1]. The right answer is [1, 4, 3, 2, 5]: node 1 stays before the range, and node 5 stays after it. Only the range may move, so the anchor node before it has to stay put.

Off by one on the range
✗ for (int i = 0; i < right - left + 1; i++) { ... } // one move too many
✓ for (int i = 0; i < right - left; i++) { ... }

The range has right − left + 1 nodes, but its first node (cur) stays in place. Only right − left nodes need to move. One extra move pulls the node after right into the range, so the list comes out wrong.

Changing the links in the wrong order
✗ prev->next = nxt; // the old front is lost here nxt->next = prev->next; // now nxt points at itself
✓ nxt->next = prev->next; // save the old front first prev->next = nxt;

prev->next is the only link to the old front of the range. If you overwrite it first, the old front and every node behind it are lost, and nxt ends up pointing at itself, which makes a cycle.

No node before position 1 when left = 1
✗ ListNode* prev = head; // when left = 1, prev is already inside the range for (int i = 1; i < left; i++) prev = prev->next;
✓ ListNode dummy(0, head); ListNode* prev = &dummy; // a node before position 1 always exists

When left = 1 the new head is a node inside the range, and nothing before it can point at the new head. The dummy gives every case a node to stitch onto, and return dummy.next returns the correct head.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.