69. Reverse Linked List II
mediumAsked at SnowflakeReverse a linked-list sub-range from position left to right, in place. Snowflake asks this to test pointer manipulation under a tight constraint — relevant for batch-flipping micro-partitions during ORDER BY DESC over partial data.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2025-Q4)— Snowflake execution-engine team uses this in onsites.
- LeetCode Discuss (2025-09)— Reported at Snowflake SDE-I screens.
Problem
Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
Constraints
The number of nodes in the list is n.1 <= n <= 500-500 <= Node.val <= 5001 <= left <= right <= n
Examples
Example 1
head = [1,2,3,4,5], left = 2, right = 4[1,4,3,2,5]Example 2
head = [5], left = 1, right = 1[5]Approaches
1. Extract, reverse, re-splice
Walk to left; cut; reverse the middle subarray; reattach.
- Time
- O(n)
- Space
- O(1)
// outline only — two phases, easy to off-by-one.Tradeoff: Works but multi-phase, error-prone.
2. One-pass in-place head-insert (optimal)
Use dummy head. Advance prev to position left-1. Then repeatedly move the node AFTER curr to right AFTER prev, reversing in place.
- Time
- O(n)
- Space
- O(1)
function reverseBetween(head, left, right) {
const dummy = { val: 0, next: head };
let prev = dummy;
for (let i = 0; i < left - 1; i++) prev = prev.next;
const curr = prev.next;
for (let i = 0; i < right - left; i++) {
const moved = curr.next;
curr.next = moved.next;
moved.next = prev.next;
prev.next = moved;
}
return dummy.next;
}Tradeoff: Single pass. The head-insert trick reverses without an explicit reverse subroutine.
Snowflake-specific tips
Snowflake interviewers want the head-insert pattern. Bonus signal: pivot to reverse-in-groups-of-k (LC 25) and discuss how it underpins batch-direction flipping in their executor when an ORDER BY direction needs to be reversed for downstream operators.
Common mistakes
- Off-by-one navigating to position left (loop right-1 vs left-1).
- Forgetting that 'moved' is curr.next, not prev.next.
- Skipping the case where left == right (must still work — no iterations).
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Reverse in groups of k (LC 25).
- How does Snowflake batch-flip rows for ORDER BY DESC?
- Iterative vs recursive trade-offs.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why head-insert?
By inserting each subsequent node right after prev, we reverse the order naturally — the first inserted ends up at the back, the last at the front.
Why is curr fixed?
curr is the node AT position left. After all iterations, it ends up at the right end of the reversed segment. The 'moved' pointer is what changes each iteration.
Practice these live with InterviewChamp.AI
Drill Reverse Linked List II and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →