Pattern this teaches:Linked List Manipulation
Reverse Linked List II
A medium Linked List interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Linked List
- Sheets
- 1
- Core for
- 4 roles
The problem
Reverse nodes of a linked list from position m to position n (1-indexed) in one pass.
Example 1
- Input
- [6,2,8,4,9], m=2, n=4
- Output
- [6,4,8,2,9]
Example 2
- Input
- [8], m=1, n=1
- Output
- [8]
Example 3
- Input
- [1,2,3], m=1, n=3
- Output
- [3,2,1]
Constraints
- 1 <= n <= 500
- 1 <= m <= n <= list.length
How to think about it
Updated 2026-09-29Reversing a subsegment only requires repositioning nodes within the window. Keep the node directly before the subsegment fixed; on each step, pluck the node following the subsegment's head and insert it immediately after that fixed boundary, sliding the window forward in a single pass.
Approaches, worst first
Extract, reverse, and reconnect
time O(n) · space O(1)
Navigate to position m, sever the sublist, reverse it using three-pointer reversal until position n, and then reconnect the severed boundaries to the surrounding list. Requires tracking four boundary pointers simultaneously.
Head insertion within windowWrite this one
time O(n) · space O(1)
Place a dummy before head and walk `pre` to index `m - 1`. Designate `start = pre.next`. For each of the `n - m` steps, extract `then = start.next`, link `start.next = then.next`, and shift `then` to front: `then.next = pre.next` and `pre.next = then`.
Where people lose marks · 3
- When `m == 1`, the subsegment starts at the head; without a dummy node, updating the true head of the list requires separate special-cased logic.
- When `m == n`, no reversals should take place; loops must exit cleanly without corrupting existing links.
- Off-by-one errors when advancing to position m: 1-indexed numbering requires `m - 1` steps from dummy to land on the predecessor.
The theory behind it
Linked List — the ground this problem stands on. All Linked List problems
What Linked List is
A linked list is a chain of separate cargo cars connected by coupling hooks, scattered anywhere across memory rather than sitting in a tidy contiguous row. Each car, called a node, holds a single piece of data and a pointer directing traffic to the address of the next car in line. Because nodes connect only by directional links, jumping straight to the tenth car is impossible without walking past the first nine.
When to reach for it
Choose a linked list when a problem requires frequent insertions and deletions at known positions without shifting whole blocks of surrounding memory. Problems mentioning pointer splicing, reversing subsequences in place, merging sorted streams, or detecting cycles in linear chains strongly point here. It is ideal when total capacity is unpredictable and memory allocation must happen one individual node at a time.
How the pattern works
Think in terms of pointer rewiring before dereferencing. Keep a dummy head node pointing to the start of the list so modifications to the initial item do not require separate edge logic. Always save references to neighboring nodes into temporary variables before cutting or redirecting forward links. When diagnosing loops or locating middle nodes, advance two references simultaneously at differing velocities so traversal completes without supplementary storage.
More Linked List problems
Target Roles
Core requirement for 4 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .