Easy · Pointer Rewiring
Understand → Visualise → Practice.
A singly linked list only knows the way forward. To reverse it, turn every next pointer around without losing the rest of the list.
Walk the list with two pointers, prev and curr. Before flipping curr.next, save where it pointed.
nxt = curr.next.
curr.next = prev.
prev = curr, curr = nxt.
O(n) time and O(1) space. A recursive version also works but uses O(n) stack space.
If you flip curr.next before saving it, the rest of the list is lost.
Read the full guide first: Linked Lists.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.