Visualize

Pattern visualizer

Three-pointer reversal

Flipping a node's arrow overwrites the only link to the rest of the list, so the rest of the chain must be saved off before you make the cut — that's the entire reason a third pointer exists. Walk forward one node at a time: stash what comes next, point the current node backward into the already-reversed prefix, then slide forward and repeat, so prev trails the reversed part while curr is the node currently being flipped. Reach for it whenever you must reverse links with O(1) extra space. Animated on: Reverse the singly linked list 1->2->3->4 in place.

Linked List

step 1 / 15
1
[0]
2
[1]
3
[2]
4
[3]
line 1

Input: 1->2->3->4, every arrow points right. Goal: flip all arrows so 4 becomes the head.

Pseudocode
1FUNCTION reverse(head):
2 prev = nothing (the reversed part is empty)
3 curr = head
4 WHILE curr is not empty:
5 next = the node after curr
6 make curr point backward to prev
7 prev = curr
8 curr = next
9 END WHILE
10 RETURN prev
11END FUNCTION

← / → step · space play · Home restart

Where to practice Linked List