Reverse Full List
The fundamental technique: prev = null, curr = head. For each node: save next = curr.next, flip curr.next = prev, advance prev = curr, curr = next. When curr is null, prev is the new head.
How It Works
Reversing a singly linked list in place means flipping every next pointer to face backward. Track two pointers: prev (initially null) and curr (initially head). Each iteration performs four assignments in strict order — save next = curr.next, flip curr.next = prev, advance prev = curr, advance curr = next. Saving next first is essential, because flipping the pointer destroys the only route forward. When curr reaches null, prev holds the new head.
The loop is O(n) time and O(1) space, versus O(n) extra space for copying values into an array or reversing via recursion. This four-line pattern is the backbone of sublist reversal, k-group reversal, palindrome checks, and list reordering, so it must be automatic.
Step-by-Step Visualization
Code
static ListNode reverseList(ListNode head) {
ListNode prev = null, curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev; // New head
}
// 1→2→3→4→null becomes 4→3→2→1→nullTips & Gotchas
Practice Problems
- 1Reverse Linked List
- 2Palindrome Linked List
- 3Reorder List
- 4Add Two Numbers II
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
What goes wrong if the assignment order is shuffled?
Flipping curr.next before saving it orphans the rest of the list — there is no pointer left to the unvisited nodes. The invariant to keep in mind: prev heads the already-reversed portion, curr heads the untouched remainder, and next is the lifeline connecting you to that remainder.
Is the recursive reversal worth knowing?
Yes, as a follow-up: recurse to the tail, then on the way back set head.next.next = head and head.next = null. It is elegant but costs O(n) call-stack space and risks overflow on long lists, so the iterative version is the default in production and interviews alike.