Intuition
Reversing a singly linked list iteratively involves changing the next
pointer of each node to point to its previous node. We need to keep track of three pointers:
prev
: This pointer tracks the node that was just processed. It starts asnull
.curr
: This pointer points to the current node being processed. It starts at thehead
.temp
: This temporary pointer is crucial to store thenext
node ofcurr
*before* we modifycurr.next
, preventing us from losing the rest of the list.
The process repeats: save curr.next
, point curr.next
to prev
, then advance prev
to curr
and curr
to the saved next node.
Step-by-Step Code
Live Visualization & Controls
Linked List
Click "Run Visualization" to start
Algorithm State
prev: null
curr: null
temp: null