Iterative three-pointer reversal
JavaScriptfunction reverse(head) {
let prev = null;
let current = head;
while (current !== null) {
const nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
return prev;
}Explanation
nextTemp stores the original next node before the link is changed. current.next is then flipped to point backward, and the pointers move one step forward. At the end, prev becomes the new head.
Output
For 1 -> 2 -> 3 -> 4 -> 5 -> null, the result becomes 5 -> 4 -> 3 -> 2 -> 1 -> null
Real-life Example
If a one-way route map needs to be traversed in the opposite direction, every arrow must be flipped carefully without breaking the chain.