92. Reverse Linked List II - Detailed Explanation
Problem Statement
Given the head of a singly linked list and two integers left and right where 1 ≤ left ≤ right ≤ n (the list length), reverse the nodes of the list from position left to position right, and return the reversed list.
Examples
Input: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]
Explanation: Nodes 2→3→4 are reversed to 4→3→2.
Input: head = [5], left = 1, right = 1
Output: [5]
Explanation: Single node, no change.
Input: head = [1,2,3], left = 1, right = 2
Output: [2,1,3]
Constraints
- The number of nodes in the list is n.
- 1 ≤ n ≤ 500
- -500 ≤ Node.val ≤ 500
- 1 ≤ left ≤ right ≤ n
Hints
- Can you copy node values into an array, reverse the subarray, then rebuild the list?
- How would you reverse a sublist in‑place by re‑linking pointers one by one?
- Could you use recursion to reverse the first k nodes, then generalize for arbitrary left/right?
Approach 1 – Brute Force via Array
Explanation
Extract all values, reverse the slice from left−1 to right−1, then rebuild the linked list from that array. Easy to implement but uses O(n) extra space.
Step‑by‑step Walkthrough
For [1,2,3,4,5], left=2, right=4:
- Extract →
[1,2,3,4,5]
- Reverse slice indices 1–3 →
[1,4,3,2,5]
- Rebuild list → 1→4→3→2→5
Python Brute Code
Java Brute Code
Approach 2 – In‑Place Iterative Reversal
Explanation
Perform one‑pass reversal by “head‑insertion” within the sublist. Use a dummy node to simplify edge cases, locate the node before position left, then iteratively take the next node and insert it immediately after the “pre” node.
Step‑by‑step Walkthrough
For 1→2→3→4→5, left=2, right=4:
- Create dummy→1→2→3→4→5, set pre=dummy, move pre 1 step to node 1.
- Let start=pre.next (2), then then=start.next (3).
- Repeat right−left times (2 iterations):
- Iter 1: detach 3 → link after pre: dummy→1→3→2→4→5; update then=start.next (4)
- Iter 2: detach 4 → link after pre: dummy→1→4→3→2→5
Return dummy.next → 1→4→3→2→5.
Visual Diagram
dummy→1→2→3→4→5
↑pre, start=2, then=3
After first insert:
dummy→1→3→2→4→5
↑pre, start=2, then=4
After second insert:
dummy→1→4→3→2→5
Python Iterative Code
Java Iterative Code
Approach 3 – Recursive Reversal
Explanation
Use a helper reverseN(head, n)
that reverses the first n nodes and returns the new head, keeping track of the successor node. To reverse between left and right, recursively skip left−1 nodes, then call reverseN
on the sublist of length right−left+1.
Outline
- If left == 1, call
reverseN(head, right)
→ handles reversal of first right nodes. - Else, let head.next = reverseBetween(head.next, left−1, right−1); return head.
- In
reverseN
, on base n==1 record successor=head.next, return head; on unwind, flip pointers.
Complexity Analysis
- Time: O(n) — each node visited a constant number of times.
- Space:
- Brute O(n) extra
- Iterative O(1) extra
- Recursive O(n) call stack in worst case
Common Mistakes
- Not handling left==right early, causing extra work.
- Failing to reconnect the tail of the reversed sublist to the successor.
- Off‑by‑one when moving the pre pointer (should move exactly left−1 times).
Edge Cases
- Single‑node list or left==right → return original head.
- Entire list reversal (left=1, right=n).
- Small lists of length 2 or 3.
Alternative Variations
- Reverse every k‑group sublists (LeetCode 25).
- Reverse nodes in even/odd positions separately.
- Rotate linked list by k places (LeetCode 61).
Related Problems
GET YOUR FREE
Coding Questions Catalog