An interactive visualization of the iterative linked list reversal algorithm. Watch how three pointers โ prev, curr, and next โ work together to reverse all the links in a single pass.
Linked List LeetCode 206 โ Easyhead of a singly linked list, reverse the list, and return the reversed list.next pointer to the following node (or None for the last node).
Reverse all the next pointers so that the last node becomes the new head and the original head points to None.fun reverseList(head: ListNode?): ListNode? { var prev: ListNode? = null var curr = head while (curr != null) { val nextNode = curr.next // save next curr.next = prev // reverse pointer prev = curr // advance prev curr = nextNode // advance curr } return prev
To reverse a linked list in-place, we only need to change where each node's next pointer points.
Instead of pointing forward, each node should point backward to the previous node.
We can do this in a single pass using three pointers: prev, curr, and next_node.
The trick is saving the next reference before overwriting it, so we don't lose our place in the list.
prev = None, curr = headnext_node = curr.next before we break the linkcurr.next = prev โ this reverses the arrowprev = curr โ prev catches up to currcurr = next_node โ curr moves to the saved next nodecurr is None (end of list)prev is now the new head of the reversed listWe visit each node exactly once, and we only use three pointer variables โ no extra data structures or recursion stack needed.