DSA · Linked List · #149
Reverse Linked List II
Module 42 · difficulty 3/5·⏱ 30:00starts on first keystroke
Given the `head` of a singly linked list and two integers `left` and `right` where `1 <= left <= right <= n`, reverse the nodes of the list from position `left` to position `right` (1-indexed, inclusive) and return the reversed list's `head`. Implement `reverseBetween(head, left, right)`. Each node is `{ val, next }`. Aim to do it in one pass.
Examples
head = [1,2,3,4,5], left = 2, right = 4→[1,4,3,2,5]— Nodes at positions 2..4 (values 2,3,4) are reversed.head = [5], left = 1, right = 1→[5]— Single node, range of length 1 — list is unchanged.head = [3,5], left = 1, right = 2→[5,3]— Reversal starts at the head, so the head changes.
Constraints
- · The number of nodes n is in the range [1, 500].
- · -500 <= Node.val <= 500
- · 1 <= left <= right <= n
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.