Reverse a Linked List
EasyThree pointers, one flipped link per step. The whole trick is saving the next node before you overwrite it.
three pointersin place
TimeO(n)
SpaceO(1)
InputNode values in order
cur
1
2
3
4
5
Start with prev as null and cur at the head.
flips0
1 / 17
prev = null; cur = head
while cur:
nxt = cur.next // save it first
cur.next = prev // flip the link
prev = cur
cur = nxt
return prev
Why it matters
Every list problem that rewires pointers is a variation of this loop.
Keyboard
space play← → stepr restartctrl K search
More in Linked List
Next visualiser
Floyd Cycle Detection