Reverse Linked List II Visualization & Animation

Reverses only a sublist from position left to right in-place using the head-insertion trick; O(n).

## What is it? Reverse only the nodes of a linked list from position `left` to position `right` (1-indexed) in-place. ## How it works - Use a dummy node before `head` - Move to the node just before position `left` — call it `pre` - Reverse the sublist from `left` to `right` using the standard reversal algorithm - Reconnect: `pre.next` → new head of reversed sublist; tail of reversed sublist → node after position `right` **One-pass "head insertion" trick:** - For each node in the range, detach it and insert after `pre` — this naturally reverses the sublist ## When to use - Reverse k-group problems (generalization) - In-place sublist manipulation - Rotation and reorder problems on linked lists ## Key Points - O(n) time, O(1) space - The dummy node eliminates the edge case where `left == 1` - Be careful with boundary connections to avoid losing nodes

Category: algorithms

Difficulty: intermediate

Time Complexity: O(n)

Space Complexity: O(1)

Reverse Linked List II

intermediate

Reverses only a sublist from position left to right in-place using the head-insertion trick; O(n).

Initializing
prev
null
curr
null
next
null
null138475NULL221563492141567prevcurrnexthead
Reverse nodes from position 2 to 4. Walk 1 step to find the pre-node just before position 2.