Reversing a linked list is one of the cleanest pointer problems in DSA. It looks simple, but it teaches an important lesson: when we change links in place, the order of updates matters more than the updates themselves.
Problem 1: Reverse Linked List Iteratively
Problem description: Given the head of a singly linked list, reverse the list and return the new head.
Example:
- Input:
1 -> 2 -> 3 -> 4 -> 5 - Output:
5 -> 4 -> 3 -> 2 -> 1
What we are solving actually:
We are not just “reading the list backward.” We are changing the direction of every next pointer so the list can be traversed from the old tail back toward the old head. The tricky part is making those changes without losing access to the remaining nodes.
What we are doing actually:
- Keep
prevas the head of the already reversed portion. - Keep
currentas the node we are currently processing. - Save
current.nextinnextNodebefore changing anything. - Reverse one link by pointing
current.nexttoprev. - Move all pointers one step forward and repeat.
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode current = head;
while (current != null) {
ListNode nextNode = current.next; // Save the rest of the list before rewiring.
current.next = prev; // Reverse the current link.
prev = current; // Current node becomes the new front of reversed part.
current = nextNode; // Continue with the untouched remainder.
}
return prev; // Prev ends at the new head.
}
}
Why This Works
At any moment:
prevpoints to a correctly reversed partial listcurrentpoints to the first node not processed yet- everything after
currentis still in its original order
Each loop iteration moves exactly one node from the original part into the reversed part. Because we save nextNode first, we never lose the remaining list.
Dry Run
For 1 -> 2 -> 3 -> 4 -> 5:
- Start with
prev = null,current = 1 - Save
nextNode = 2, set1.next = null, moveprev = 1,current = 2 - Save
nextNode = 3, set2.next = 1, moveprev = 2 -> 1,current = 3 - Save
nextNode = 4, set3.next = 2, moveprev = 3 -> 2 -> 1,current = 4 - Continue until
current = null
At the end, prev points to 5 -> 4 -> 3 -> 2 -> 1, which is the answer.
Common Mistakes
- Forgetting to save
nextNodebefore changingcurrent.next - Returning
headinstead ofprev - Updating
currentbefore reversing the pointer - Thinking a second pass is needed when one pass is enough
Debug Steps
Debug steps:
- print
prev,current, andnextNodeat each iteration - test
null, one-node, and two-node lists first - check that no node is skipped and no cycle is created
- verify that the final returned node is the old tail
Boundary Cases
- empty list returns
null - single node returns the same node
- two nodes should simply swap direction
- large lists still work in one pass with constant extra space
Complexity
- Time:
O(n) - Space:
O(1)
Why Iterative Reversal Is a Core Pattern
This same pointer update pattern appears in:
- reverse a sublist
- reverse nodes in
kgroups - reorder list style problems
- palindrome linked-list checks
So this is more than one interview question. It is a reusable linked-list building block.
Key Takeaways
- iterative reversal is controlled pointer rewiring, not value swapping
- the order
save next,reverse link,move pointersis the whole algorithm prevbecomes the new head after the traversal finishes
Comments