Reverse Linked List in Java: Iterative, Recursive, and Stack Approaches
-
Last Updated: October 31, 2026
-
By: javahandson
-
Series

Reverse Linked List in Java explained three ways — stack, recursion, and the O(1) three-pointer loop. Full 7-node dry runs and interview-ready code.
Reverse Linked List in Java is the first real pointer puzzle most beginners meet. The list points one way, and your job is to make it point the other way. So the last node becomes the first, and the first node becomes the last.
The task sounds tiny. You get the head of a singly linked list. You return the head of the same list, reversed.
There is a catch that trips people up. A linked list has no index you can jump to. You can only follow the next arrow forward. So you must flip those arrows carefully, or the rest of the chain goes missing.
We build the answer in three steps, as always. A stack is the easy first idea, because it reverses order for free. Recursion is the next step up, and it reads beautifully once it clicks. The three-pointer loop is the lean version that interviewers love, since it uses no extra memory at all.
Every approach gets a full, step-by-step dry run on the same seven-node list. Nothing is skipped here, so you can watch each arrow flip and see exactly how the head ends up on the other side.
Let us pin down the rules before we write any code.
1 → 2 → 3 → 4 → 5 → 6 → 7.7 → 6 → 5 → 4 → 3 → 2 → 1.next pointer to the node after it.null, which marks the end of the list.After reversing, the old tail becomes the new head. Also the old head becomes the new tail, so its next must now point to null. We will trace the same seven-node list through every approach, so the before and after stay fixed in your mind.
A singly linked list is a chain of small boxes called nodes. Each node keeps two things: a value, and a pointer to the next node. You hold on to the first node, the head, and from there you can reach every other node by following next.
val field stores the data, such as a number.next field stores a link to the following node, or null at the very end.Here is the node class we use across all three approaches.
class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
}Right now every arrow points forward, from a node to the one after it. To reverse the list, we flip each arrow so it points backward instead. That is the whole idea, and each approach just flips those arrows in its own way.
One detail matters a lot. The moment you flip an arrow, you lose the link you were about to follow. So you must save the next node first, or the rest of the list simply vanishes.
A stack hands back items in reverse order, last in first out. So if we push every node onto a stack, the last node sits on top. We then pop them one by one and link them front to back, which builds the reversed list. It is the most beginner-friendly idea, even if it spends extra memory.
if head is null or head.next is null:
return head
stack = empty stack
node = head
while node is not null: // push every node
push node onto stack
node = node.next
newHead = pop from stack // old tail, now the head
tail = newHead
while stack is not empty: // pop rest, link forward
tail.next = pop from stack
tail = tail.next
tail.next = null // close the new list
return newHeadThink of the stack as a pile of plates. The last plate you drop on is the first one you pick up. We use that flip to our advantage.
null, so the new list has a clean end instead of a loop.import java.util.ArrayDeque;
import java.util.Deque;
public class ReverseListStack {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
Deque<ListNode> stack = new ArrayDeque<>();
ListNode node = head;
while (node != null) {
stack.push(node);
node = node.next;
}
ListNode newHead = stack.pop();
ListNode tail = newHead;
while (!stack.isEmpty()) {
tail.next = stack.pop();
tail = tail.next;
}
tail.next = null;
return newHead;
}
}The Java code follows the plate-pile plan directly. The one helper to know is ArrayDeque, which Java offers as a fast stack through push and pop.
null head or a lone node returns right away.node that starts at the head.node forward until it falls off the end.newHead.tail, moving tail along each time.next to null, which closes the list so it does not point back into itself.Let us trace the list 1 → 2 → 3 → 4 → 5 → 6 → 7. The head is node 1, so the tiny-case check passes and we move on to pushing. The first table shows the push pass, where the stack grows one node at a time.
| step | node.val pushed | stack (bottom → top) |
|---|---|---|
| 0 | 1 | [1] |
| 1 | 2 | [1, 2] |
| 2 | 3 | [1, 2, 3] |
| 3 | 4 | [1, 2, 3, 4] |
| 4 | 5 | [1, 2, 3, 4, 5] |
| 5 | 6 | [1, 2, 3, 4, 5, 6] |
| 6 | 7 | [1, 2, 3, 4, 5, 6, 7] |
Now node is null, so the push loop ends. Node 7 sits on top, which is exactly the node we want as the new head. The next table shows the pop pass, where we pull nodes off the top and link them forward.
| step | popped node | action | reversed list so far |
|---|---|---|---|
| set head | 7 | newHead = 7, tail = 7 | 7 |
| 1 | 6 | tail.next = 6, tail = 6 | 7 → 6 |
| 2 | 5 | tail.next = 5, tail = 5 | 7 → 6 → 5 |
| 3 | 4 | tail.next = 4, tail = 4 | 7 → 6 → 5 → 4 |
| 4 | 3 | tail.next = 3, tail = 3 | 7 → 6 → 5 → 4 → 3 |
| 5 | 2 | tail.next = 2, tail = 2 | 7 → 6 → 5 → 4 → 3 → 2 |
| 6 | 1 | tail.next = 1, tail = 1 | 7 → 6 → 5 → 4 → 3 → 2 → 1 |
The stack is now empty, so the pop loop ends. We set tail.next = null on node 1, which closes the list. The final answer is 7 → 6 → 5 → 4 → 3 → 2 → 1.
Let us walk both passes and see exactly how the stack does the reversing for us.
Push pass, step 0 to step 6.
This pass only copies node references onto the stack. The original list is not changed yet, so every arrow still points forward.
[1].After the pass the pile reads [1, 2, 3, 4, 5, 6, 7] from bottom to top. The key point is that node 7 is on top, ready to become the new head.
Pop pass, set-head step.
Now we flip into rebuild mode. We pop the top node, which is node 7, and treat it as both the new head and the current tail.
newHead, so the reversed list starts at 7.tail at node 7, because the next popped node attaches right after it.Pop pass, step 1 to step 6.
Each remaining pop pulls the next-highest node and hooks it onto the tail. The list grows backward compared with the original order.
tail.next = 6, so the list becomes 7 → 6, and tail moves to 6.7 → 6 → 5.7 → 6 → 5 → 4 → 3.7 → 6 → 5 → 4 → 3 → 2 → 1.One last move matters. Node 1 was the original head, and its old next still pointed at node 2. We set tail.next = null on node 1, which cuts that stale link and gives the reversed list a clean ending.
| 💡 Interview Insight A good follow-up is “can you reverse without extra space?” The stack is clear to explain, but it holds all n nodes at once. Mention that the three-pointer loop drops the memory to O(1), which is the answer they want. |
The stack is easy to picture, yet it carries the whole list in memory at once. Recursion keeps the same linear time, and it hides the stack inside the function calls instead.
Recursion solves the list by trusting itself to reverse the smaller list first. We dive to the very end, reverse everything after the current node, then flip just one arrow on the way back. It reads short, but the real work happens as the calls unwind.
function reverse(node):
if node is null or node.next is null:
return node // base case: this is the new head
newHead = reverse(node.next) // reverse the rest first
node.next.next = node // make the next node point back
node.next = null // drop the old forward link
return newHeadThe trick is to let the function reverse the tail for you, and only worry about one link yourself. So each call fixes exactly one arrow.
null node or the last node is returned as the new head, since it ends up at the front.node.next.next = node tells the next node to point back at the current node.node.next = null removes the old forward link, which would otherwise create a loop.public class ReverseListRecursive {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
}The Java version is almost a direct copy of the pseudocode. The only part to slow down on is head.next.next, which reaches two nodes ahead to redirect a pointer.
head.next, which reverses the rest and returns that part’s head as newHead.head.next.next = head, so the node right after head now points back at head.head.next = null, which breaks the old forward link and prevents a cycle.newHead, and that same head bubbles up unchanged through every call.We trace the same list 1 → 2 → 3 → 4 → 5 → 6 → 7. Recursion has two phases: the dive down to the base case, then the unwind back up. The first table shows the dive, where each call simply calls itself on the next node.
| call | node | node.next | what happens |
|---|---|---|---|
| reverse(1) | 1 | 2 | not base, calls reverse(2) |
| reverse(2) | 2 | 3 | not base, calls reverse(3) |
| reverse(3) | 3 | 4 | not base, calls reverse(4) |
| reverse(4) | 4 | 5 | not base, calls reverse(5) |
| reverse(5) | 5 | 6 | not base, calls reverse(6) |
| reverse(6) | 6 | 7 | not base, calls reverse(7) |
| reverse(7) | 7 | null | base case, returns 7 as newHead |
Node 7 is the base case, so it becomes newHead and never changes again. Now the calls unwind in reverse order, and each one flips a single arrow. Watch the unwind pass unfold in the table below.
| returns into | rewire: node.next.next = node | then: node.next = null | list built so far |
|---|---|---|---|
| reverse(6) | 7.next = 6 | 6.next = null | 7 → 6 |
| reverse(5) | 6.next = 5 | 5.next = null | 7 → 6 → 5 |
| reverse(4) | 5.next = 4 | 4.next = null | 7 → 6 → 5 → 4 |
| reverse(3) | 4.next = 3 | 3.next = null | 7 → 6 → 5 → 4 → 3 |
| reverse(2) | 3.next = 2 | 2.next = null | 7 → 6 → 5 → 4 → 3 → 2 |
| reverse(1) | 2.next = 1 | 1.next = null | 7 → 6 → 5 → 4 → 3 → 2 → 1 |
The outermost call reverse(1) finishes last and returns newHead, which is still node 7. The final answer is 7 → 6 → 5 → 4 → 3 → 2 → 1.
Let us go phase by phase and watch the calls stack up, then peel back down.
Dive phase, reverse(1) down to reverse(7).
This phase does no rewiring at all. Each call only checks the base case, and then calls itself on the next node, going deeper.
reverse(1) sees that node 1 has a next, so it pauses and calls reverse(2).reverse(2) calls reverse(3), and the chain keeps diving one node at a time.reverse(7) the node’s next is null, so this is the base case, and it returns node 7.At this moment seven calls are paused in memory, stacked from node 1 at the bottom to node 7 at the top. Node 7 is now locked in as the new head.
Unwind step, back into reverse(6).
We return into the call that was waiting on node 6. Here node is 6 and node.next is 7, so the rewiring begins.
node.next.next = node becomes 7.next = 6, so node 7 now points to node 6.node.next = null becomes 6.next = null, which drops node 6’s old forward link to node 7.The reversed part now reads 7 → 6, and node 7 stays the head. That stale link from 6 had to go, or 6 and 7 would point at each other in a loop.
Unwind steps, back into reverse(5) through reverse(2).
Each of these calls repeats the exact same two lines, one level higher. The reversed chain grows by one node every time.
reverse(5) the rewire 6.next = 5 extends the chain to 7 → 6 → 5, and 5.next = null.reverse(4) the rewire 5.next = 4 gives 7 → 6 → 5 → 4, and 4.next = null.reverse(3) and reverse(2) add nodes 3 and 2, so the chain reaches 7 → 6 → 5 → 4 → 3 → 2.Unwind step, back into reverse(1).
The last call to finish is the first one that started. Here node is 1 and node.next is 2.
2.next = 1 attaches node 1 at the end, so the list becomes 7 → 6 → 5 → 4 → 3 → 2 → 1.1.next = null marks node 1 as the new tail, which ends the list cleanly.Every call returned the same newHead, node 7, without touching it. So the outermost call hands node 7 back to you as the reversed list.
| 💡 Interview Insight Interviewers often ask “what is the space cost of the recursive version?” It is O(n), not O(1). Each paused call sits on the call stack, so a very long list can even cause a stack overflow. |
Recursion is elegant and short to write. Still, the hidden call stack costs the same memory as the explicit stack did. The three-pointer loop fixes that by reversing the list as it walks, with nothing extra saved.
This is the clean, memory-light answer. We walk the list once with three pointers: prev, curr, and a saved nextNode. At each node we flip its arrow to point at prev, then shuffle all three pointers forward. No stack, no recursion, just a single loop.
prev = null
curr = head
while curr is not null:
nextNode = curr.next // save the link before we lose it
curr.next = prev // flip this node's arrow backward
prev = curr // prev moves up to curr
curr = nextNode // curr moves up to the saved node
return prev // prev is the new headPicture three fingers on the list. One finger trails behind, one sits on the current node, and one saves the road ahead. We move them along together, flipping one arrow per step.
prev at null, because the new tail must point to null once it is flipped.curr begins at the head, which is the first node we flip.curr.next into nextNode, so the rest of the list is not lost when we flip the arrow.curr.next = prev reverses the current node’s link to point backward.prev and curr step forward by one, ready for the next node.curr reaches null, the loop ends, and prev is sitting on the last node, the new head.public class ReverseListIterative {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextNode = curr.next;
curr.next = prev;
prev = curr;
curr = nextNode;
}
return prev;
}
}The Java code is short because one loop does all the work. The order of the four lines inside the loop is the whole trick, so keep them in exactly this sequence.
prev starts at null, and curr starts at the head.curr still points at a real node.curr.next into nextNode before anything is changed, which protects the rest of the list.curr.next = prev, the single line that actually reverses the link.prev and curr forward, so the next loop works on the next node.prev, which lands on the old tail after the loop, and that is the new head.We trace the same list 1 → 2 → 3 → 4 → 5 → 6 → 7 one more time. The table shows all three pointers at each turn of the loop, plus the one arrow we flip. Read prev before as the value of prev when the step starts.
| iter | curr | nextNode saved | prev before | flip: curr.next = prev | prev after |
|---|---|---|---|---|---|
| 0 | 1 | 2 | null | 1.next = null | 1 |
| 1 | 2 | 3 | 1 | 2.next = 1 | 2 |
| 2 | 3 | 4 | 2 | 3.next = 2 | 3 |
| 3 | 4 | 5 | 3 | 4.next = 3 | 4 |
| 4 | 5 | 6 | 4 | 5.next = 4 | 5 |
| 5 | 6 | 7 | 5 | 6.next = 5 | 6 |
| 6 | 7 | null | 6 | 7.next = 6 | 7 |
After iteration 6, curr becomes null, so the loop ends. At that point prev is on node 7, and every arrow has been flipped. The method returns node 7, giving 7 → 6 → 5 → 4 → 3 → 2 → 1.
Let us step through the loop and watch the three pointers crawl along the list.
Iteration 0, flipping node 1.
We begin with prev at null and curr on node 1, the head. This first step turns the old head into the new tail.
nextNode = curr.next, so nextNode points at node 2 and the rest of the list is safe.curr.next = prev sets 1.next = null, which flips node 1’s arrow to point at nothing.prev moves to node 1, and curr moves to node 2, the saved node.Node 1 now points to null, which is correct, because it is the new tail of the reversed list.
Iteration 1, flipping node 2.
Now curr sits on node 2, and prev sits on node 1. We flip node 2 to point back at node 1.
nextNode = curr.next, so nextNode points at node 3 before we change anything.curr.next = prev sets 2.next = 1, so node 2 now points back to node 1.prev lands on node 2, and curr lands on node 3.The reversed part so far reads 2 → 1 → null, and it keeps growing from the front as we go.
Iterations 2 to 5, flipping nodes 3 through 6.
These four steps all repeat the same four lines. Each one saves the road ahead, flips one arrow, and moves the pointers forward.
3.next = 2 grows the reversed chain to 3 → 2 → 1.4.next = 3 extends it to 4 → 3 → 2 → 1.6 → 5 → 4 → 3 → 2 → 1.nextNode first, so the untouched tail of the list is never lost.Iteration 6, flipping node 7.
This is the last real node. Here curr is on node 7, and prev is on node 6.
nextNode = curr.next, which is null, since node 7 was the original tail.7.next = 6 points node 7 back at node 6, finishing the full chain.prev moves to node 7, and curr moves to null, the saved value.Because curr is now null, the while condition fails and the loop stops. The pointer prev is resting on node 7, so we return it as the new head.
| 💡 Interview Insight A classic trap is forgetting to save nextNode before the flip. If you write curr.next = prev first, you lose the link to the rest of the list. State the save-first order out loud, because interviewers watch for it. |
This version matches the others on speed and beats both on memory. It walks the list once and reverses it in place, which is why it is the expected interview answer.
All three return the same reversed list. They just pay different prices in memory and clarity.
| Approach | Time | Space | Note |
|---|---|---|---|
| Stack | O(n) | O(n) | Easy to picture, but stores every node |
| Recursion | O(n) | O(n) | Short and elegant, uses the call stack |
| Three pointers | O(n) | O(1) | Fast and lean, the expected answer |
All three share the same linear time, so the real difference is space. The stack and the recursion both hold n items at once, one on a real stack and one on the call stack. The three-pointer loop keeps only three references, which is why it wins on memory.
In an interview, mention the stack first to show the idea, then recursion to show you know it reads cleanly. Land on the three-pointer loop as your final answer, and call out its O(1) space. That progression tells a clean story from simple to optimal.
| 💡 Interview Insight If pushed on why the loop is O(1), point out that prev, curr, and nextNode are just three pointers. Their count never grows, however long the list gets, so the extra space stays constant. |
A few small slips catch beginners on Reverse Linked List. Keep these in mind.
curr.next before saving the next node loses the rest of the list instantly.next to null leaves a loop between the last two nodes.head instead of prev in the loop version hands back the old tail, pointing the wrong way.head is null, should return null without error.Run the empty list and the single-node list through your code before you call it done. Those two tiny cases catch more bugs than any long list will.
A: The iterative three-pointer loop is the most efficient. It walks the list once and flips each next pointer in place, giving O(n) time and O(1) extra space — no stack or recursion needed.
A: Once you set curr.next = prev, the link to the rest of the list is gone. Saving curr.next into nextNode first keeps the road ahead, so you can keep walking after the flip.
A: It is O(n), not O(1). Each recursive call pauses on the call stack until the base case returns, so a very long list can even trigger a stack overflow.
A: Push every node onto a stack, then pop them one by one. Because a stack is last-in-first-out, the old tail comes out first and becomes the new head, and you relink each popped node forward.
A: An empty list (head is null) returns null, and a single node is already its own reverse, so it comes back unchanged. Both are handled by the base-case check at the top of the method.
Reverse Linked List in Java looks small, yet it teaches the core habit of every pointer problem: save the next link before you change the current one. Once that clicks, the arrows stop being scary.
Our seven-node traces showed the payoff clearly. The stack pushed every node and popped them in flipped order. Recursion dove to the end and flipped one arrow per return, while the three-pointer loop flipped arrows as it walked.
So take the pattern, not just the answer. When you must rewire a list, hold three pointers and move them in a fixed order. Reach for the loop when memory matters, and keep recursion handy when a clean, short solution reads better.
That save-then-flip habit turns many linked list problems from tricky into routine. It will carry you straight into the harder list problems waiting next in the series.