Reverse Linked List
Reverse Linked List is LeetCode 206 (Easy). You get the head of a list and must reverse a singly linked list in place, then return the new head.
- The list can be empty, and holds at most 5000 nodes.
- The follow-up asks for two versions: one with a loop and one with recursion.
- The number of nodes in the list is in the range [0, 5000].
- -5000 <= Node.val <= 5000
Intuition
Nothing has to move. Each node already sits in memory; only its next pointer decides the order. Reversing the list means turning every next pointer around so it points at the node before it.
Walking forward, the node before is always known, because it is the one you just left. That makes a single pass enough. The only danger is order of work: the moment a node's next is overwritten, the unvisited part of the list is cut off. So each round remembers the next node first, then turns the pointer, then steps forward.
Any task that walks a singly linked list backwards, or needs its second half in reverse (palindrome check, reorder, k-group reversal), starts from this three-pointer loop.
Approach
Before reading on, write the loop with three variables: the node behind, the current node, and the node ahead. Decide which line must come first in the loop body, and what the loop returns when the list is empty.
Loop or recursion
Turns one pointer per round while walking forward.
- Constant memory: three variables, whatever the length.
- No depth limit: 5000 nodes is just 5000 rounds.
The version to write first.
Reverses the rest of the list first, then hooks the current node onto its end.
- Short: four lines once the idea clicks.
- One call per node: the call stack grows with the list.
Asked for by the follow-up.
Both read every node once. The loop is the one traced below; the recursive one follows the demo.
Start with nothing behind the head
Set prev = None and curr = head. The old head will end up as the last node, and the last node must point to null, so the first pointer written should be null, which is what prev holds.
Remember the node ahead
Store nxt = curr.next before changing anything. It is the only reference to the part of the list that has not been reversed yet.
Point the current node backwards
Set curr.next = prev. The current node is now the front of the reversed part, which grows by one node each round.
Step both pointers forward
Set prev = curr and curr = nxt, and repeat while curr is not null. When curr falls off the end, every pointer has been turned and prev stands on the old tail, so return prev.
Code in Python, C++ and Java
Recursion
Trust the call on head.next to reverse everything after head and return the new head (the old tail). At that point head.next still points at the node that is now the end of the reversed part, so head.next.next = head hangs head after it, and head.next = None makes head the new end. A list of 0 or 1 nodes is already reversed. Each node adds one stack frame, and Python allows only 1000 by default, so a 5000-node list needs sys.setrecursionlimit outside a judge that raises it.
Common pitfalls
Turning the pointer before saving the next node
curr.next = prev prev, curr = curr, curr.next
nxt = curr.next curr.next = prev prev, curr = curr, nxt
After the first line, curr.next is prev, so curr steps backwards instead of forwards. On [1, 2, 3] the first round moves curr to null and the answer is [1]; nodes 2 and 3 are lost.
Returning head instead of prev
return head
return prev
head still names the old first node, which is now the last one and points to null. On [1, 2, 3] this returns [1].
Recursive version: leaving the old link in place
head.next.next = head return new_head
head.next.next = head head.next = None return new_head
Without head.next = None, the original head still points at the second node while the second node points back at it. On [1, 2] the result is 2 → 1 → 2 → 1 …, a cycle that never ends.
Edge cases
curr starts as null, the loop never runs, and prev (null) is returned.
One round: its next becomes null (it already was), and that node is returned as the head.
Complexity
Reverse Linked List FAQ
Why not copy the values into an array and write them back in reverse?
It gives the right output but needs O(n) extra memory, and it changes the values stored in the nodes rather than the links between them. If the nodes carry other data, or other code holds references to them, that is not the same as reversing the list.
How do you reverse a doubly linked list?
Walk the list and swap each node's prev and next fields. The last node you swap becomes the new head. No separate saved pointer is needed, because the old next is still available as the new prev.
How do you reverse only part of a list, or one block at a time?
Run the same loop on the section, then reconnect its ends to the nodes on either side. Doing that for every block of k nodes is Reverse Nodes in k-Group.