Problem Walkthrough

Reverse Linked List II LeetCode 92 Guide

Master one-pass in-place reversal of a sublist between positions left and right — the clean pointer technique every linked list interview demands.

9 min read|

Reverse Linked List II

LeetCode 92

Problem Overview

LeetCode 92 — Reverse Linked List II — gives you the head of a singly linked list and two integers, left and right, representing 1-indexed positions. Your task is to reverse the nodes between those two positions (inclusive) and return the modified list.

Unlike the basic Reverse Linked List (#206) which reverses the entire list, this problem asks you to reverse only a contiguous sublist while leaving the nodes before position left and after position right untouched. The challenge is wiring the reversed segment back into the surrounding list without losing any references.

The follow-up constraint — do it in one pass — rules out a naive two-pass approach where you first count the list then reverse. You must set up your pointers correctly from the start and execute the entire reversal in a single traversal of the relevant segment.

  • Constraints: 1 <= left <= right <= n (positions are valid and 1-indexed)
  • The list has at most 500 nodes; node values are in [-500, 500]
  • You must not allocate extra nodes — reverse in-place by rewiring pointers
  • Follow-up: could you do it in one pass?

Setting Up the Approach

The key setup move is to create a dummy node whose next pointer points to head. This gives you a stable node before the very first element of the list, which eliminates a special case: when left equals 1, there is no "real" node before the reversal zone. The dummy node fills that role unconditionally.

With the dummy in place, traverse forward (left - 1) steps from the dummy to land on the node just before position left. Call this pointer "pre". At this point, pre.next is the first node of the sublist to be reversed — this will become the last node after reversal.

This two-pointer setup (dummy → pre → start of reversal zone) is the universal template for linked list problems that involve inserting or rearranging nodes in the middle of a list. Internalizing it will accelerate your solution to any sublist manipulation problem.

💡

Dummy Node Trick

A dummy node eliminates the special case when reversing from the head (left = 1). By pointing dummy.next = head, your "pre" pointer always has a valid predecessor node — making the code uniform regardless of where the reversal starts.

Identifying Key Pointers

Once you have reached the pre node, identify the two critical pointers for the reversal: pre is the node immediately before the reversal zone, and cur is pre.next — the first node inside the reversal zone. After the reversal loop completes, cur will have become the last node in the reversed segment.

The crucial insight is that neither pre nor cur moves during the reversal loop. Pre stays as the anchor before the zone, and cur stays as the tail of the growing reversed sublist. Only nodes are pulled from cur.next to the front of the reversed section on each iteration.

Think of it this way: before the loop, the order is: ... → pre → cur → A → B → C → ... After the loop completes (right - left iterations), the order will be: ... → pre → C → B → A → cur → ... Pre and cur are the fixed endpoints; everything in between gets reversed by the loop.

  1. 1Create dummy node; set dummy.next = head
  2. 2Move pre forward (left - 1) times from dummy
  3. 3Set cur = pre.next (first node of reversal zone)
  4. 4Note: pre and cur will not move during the reversal loop

The Reversal Loop

The reversal loop runs exactly (right - left) times. On each iteration: grab the node at cur.next (call it "next"), detach it from cur, and insert it immediately after pre. This moves "next" to the front of the reversed sublist, pushing cur one position further back.

In code: let next = cur.next; cur.next = next.next; next.next = pre.next; pre.next = next. These four pointer assignments, applied (right - left) times, fully reverse the sublist in a single pass with O(1) extra space.

After the loop, pre.next points to the last node that was pulled (which is now the new head of the reversed segment), and cur.next points to the first node after the reversed segment. The surrounding list is automatically reconnected because cur never lost its link to the node at position right + 1.

ℹ️

Pull to Front Technique

This is the "pull to front" technique: each iteration grabs cur.next and moves it to right after pre, naturally building the reversed sublist one node at a time. No stack or extra array is needed — just four pointer reassignments per iteration.

Walk-Through Example

Consider the list 1→2→3→4→5 with left=2, right=4. We need to reverse nodes 2, 3, 4. First, create a dummy node: dummy→1→2→3→4→5. Move pre to node 1 (one step from dummy since left-1=1). Set cur = node 2.

Iteration 1: next = node 3. cur.next = node 4. next.next = node 2. pre.next = node 3. State: dummy→1→3→2→4→5. Cur is still node 2, pre is still node 1.

Iteration 2: next = node 4. cur.next = node 5. next.next = node 3. pre.next = node 4. State: dummy→1→4→3→2→5. After 2 iterations (right - left = 4 - 2 = 2), the loop ends. Return dummy.next which is node 1, giving the result: 1→4→3→2→5.

  1. 1Initial: dummy→1→2→3→4→5, pre=node1, cur=node2
  2. 2After iteration 1: dummy→1→3→2→4→5, pre=node1, cur=node2
  3. 3After iteration 2: dummy→1→4→3→2→5, pre=node1, cur=node2
  4. 4Loop ends (ran right-left=2 times); return dummy.next = node1
  5. 5Result: 1→4→3→2→5 — nodes 2,3,4 are reversed to 4,3,2

Code Walkthrough: Python and Java

In Python: def reverseBetween(head, left, right): dummy = ListNode(0, head); pre = dummy; for _ in range(left - 1): pre = pre.next; cur = pre.next; for _ in range(right - left): next = cur.next; cur.next = next.next; next.next = pre.next; pre.next = next; return dummy.next. Time complexity O(n), space complexity O(1).

In Java: public ListNode reverseBetween(ListNode head, int left, int right) { ListNode dummy = new ListNode(0, head); ListNode pre = dummy; for (int i = 0; i < left - 1; i++) pre = pre.next; ListNode cur = pre.next; for (int i = 0; i < right - left; i++) { ListNode next = cur.next; cur.next = next.next; next.next = pre.next; pre.next = next; } return dummy.head; }. The structure is identical across languages — the algorithm is purely about pointer wiring.

The beauty of this solution is its minimal footprint: one dummy node allocation, two named pointers (pre and cur), one temp pointer (next) used inside the loop, and zero auxiliary data structures. This is what interviewers mean when they ask for an in-place O(1) space solution.

⚠️

Common Mistake

cur never moves: it starts as the first node in the reversal zone and ends as the last after reversal. Only cur.next changes each iteration as it gets bypassed. A common bug is accidentally reassigning cur — your loop should only modify cur.next, next.next, and pre.next.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now