← Back to DSA Animator
Reverse Linked List II LC #92 Medium Linked List · Front-Insertion
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 (1-indexed), and return the reversed list.

Example 1
Input: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]
Example 2
Input: head = [5], left = 1, right = 1
Output: [5]
Example 3
Input: head = [1,2,3,4,5], left = 1, right = 5
Output: [5,4,3,2,1]
Constraints: 1 ≤ n ≤ 500  |  −500 ≤ Node.val ≤ 500  |  1 ≤ left ≤ right ≤ n
Try Examples
Approach
Front-Insertion (In-Place Sublist Reversal)
A dummy sentinel node is prepended. Walk prev to position left−1. Then repeatedly detach curr.next and re-insert it right after prev — this is the "insert at front" technique. One pass — O(n) time, O(1) extra space.
Linked List
Variables
left
right
pos (i)
iter
phase
Step Logic
Press ▶ Play or Next Step to begin the animation.
🎉
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Create dummy → head; set prev = dummy
2
Advance prev to position left−1 (loop i=1..left-1)
3
Set curr = prev.next — anchor for the tail of the reversed sublist
4
Loop right−left times: nxt = curr.next → insert nxt right after prev (front-insertion)
5
Return dummy.next
Time
O(n)
Space
O(1)
Why Front-Insertion Works

Each iteration grabs the node right after curr (call it nxt) and moves it to the front of the growing reversed sublist — just after prev:

curr.next = nxt.next — skip nxt from its place
nxt.next = prev.next — nxt will point to old sublist head
prev.next = nxt — nxt is the new sublist head

After right−left moves the entire sublist is reversed in-place. curr naturally ends up at the tail because it never moves — only its successor changes.