Reverse Linked List
Each node needs its arrow to point at the node before it. Walk the list once. Keep prev (the reversed part) and cur (the next node to flip). Save cur->next before each flip, so the rest of the list is not lost.
The list is reversed when every node's next points to the node before it in the original list, and the old head points to null.
Problem
You are given the head of a singly linked list. Reverse the list, so the last node becomes the first and every arrow points the other way. Return the new head.
head, the first node of a singly linked list. The list can be empty.
The head of the reversed list.
Every arrow now points back towards the start.
Only the one arrow between them flips. Node 2 now points to node 1, and node 1 points to null.
An empty list has nothing to reverse.
- • The list has between 0 and 5000 nodes.
- • -5000 ≤ Node.val ≤ 5000
- • Follow-up: can you reverse the list both iteratively and recursively?
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
If I walk the list from the head, which arrow can I flip first? What do I lose when I flip it?
- 2.
After I flip the first arrow, can I still reach the second node? How?
- 3.
After the reversal, what should the old first node point to?
- 4.
How do I know when to stop, and which pointer holds the answer at that moment?
- 5.
Could I swap values from both ends, like an array? What stops me?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Push every node on a stack, then pop them back in reverse order | O(N) | O(N) |
| Optimal | Walk once, flip each arrow back, keep prev and cur | O(N) | O(1) |
Why does this belong to this pattern?
- “reverse the list”Every arrow must flip. A linked list can't walk backwards, so we flip each arrow while walking forward.
- “singly linked list”Each node only knows its next node. Save the next node before you change an arrow.
- “return the new head”The old head becomes the last node. The new head is the last node we reach, which is the one prev holds.
This is the base of the whole reversal pattern: walk forward once and flip each arrow. Later questions reverse part of a list, or use this same move on a doubly linked list or in a number. The one rule to keep: before you change cur->next, save it. Each later question in this sub-pattern changes this basic move a little.
Don't make this mistake
After cur->next = prev, the arrow no longer points to the rest of the list. The last line reads cur->next and gets the old prev, which is null on the first node. So cur becomes null, the loop stops after one node, and the rest of the list is lost.
Reversing an array by swapping from both ends works because you can jump straight to index n − 1 − i. A singly linked list can't step backwards, so right can't move inward. On [1, 2, 3, 4, 5], the first swap trades 1 and 5. The next pair is 2 and 4, and node 4 sits just before the tail, so it can only be found again with a walk from the head. Every pair repeats that walk, which makes the whole thing O(N²).
The old head was the first node, so its next is null after the reversal. Returning it gives back a list with one node. The new head is the last node we flipped, and prev holds it.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.