Reverse Sublist
Reverse only nodes from position m to n. Navigate to position m, save the 'before' connection, reverse the sublist, then reconnect both ends. Tricky part is keeping track of the boundary nodes.
How It Works
Reversing only positions m through n combines navigation, the core reversal loop, and careful reconnection. Attach a dummy node before the head so the m = 1 case needs no special handling. Walk to the node just before position m and remember it as the anchor; its next is the sublist's first node, which will become the sublist's tail after reversal. Run the standard prev/curr reversal for exactly n − m + 1 steps. Then stitch: the anchor's next points to the reversed segment's new front (prev), and the old sublist head's next points to the first node after the segment (curr).
One pass, O(n) time, O(1) space. The entire difficulty lives in those two reconnection assignments.
Step-by-Step Visualization
Code
static ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;
for (int i = 1; i < m; i++) prev = prev.next;
ListNode curr = prev.next;
for (int i = 0; i < n - m; i++) {
ListNode next = curr.next;
curr.next = next.next;
next.next = prev.next;
prev.next = next;
}
return dummy.next;
}
// Reverse from position 2 to 4: 1→2→3→4→5 → 1→4→3→2→5Tips & Gotchas
Practice Problems
- 1Reverse Linked List II
- 2Reverse Nodes in k-Group
- 3Reorder List
- 4Swap Nodes in Pairs
About the In-Place Reversal Pattern
Reverse the direction of pointers one by one. Use three pointers: prev, current, and next. Save next, point current back to prev, then advance prev and current. After the loop, prev is the new head.
Most linked list problems are about pointer manipulation. Draw it out! Fast & slow pointers detect cycles and find midpoints. In-place reversal is the other core technique.
Common Linked List Interview Problems
- Reverse Linked List
- Merge Two Sorted Lists
- Linked List Cycle
- Remove Nth Node From End
- LRU Cache
- Reorder List
Frequently Asked Questions
Why does the dummy node matter so much here?
When m = 1 there is no real node before the sublist, so 'the node before position m' would not exist and the new head changes. A dummy gives every case a uniform anchor, and returning dummy.next yields the correct head whether or not the reversal touched position 1.
Which pointers should I identify before writing any code?
Four of them: the anchor before the sublist, the sublist's original first node (future tail), the sublist's original last node (future front), and the node after the sublist. Naming these up front — ideally on a drawing — turns the reconnection from guesswork into two obvious assignments.
There is also a head-insertion variant — how does it differ?
Instead of reversing then reconnecting, repeatedly detach the node after the sublist start and reinsert it right after the anchor, n − m times. It achieves the same result in one pass and some find it less error-prone since the list is never in a disconnected state.