Reverse Linked List in Java: Iterative, Recursive, and Stack Approaches

  • Last Updated: October 31, 2026
  • By: javahandson
  • Series
img

Reverse Linked List in Java: Iterative, Recursive, and Stack Approaches

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.

1. Introduction

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.

2. Understanding the Problem

Let us pin down the rules before we write any code.

  • You get the head node of a singly linked list, for example 1 → 2 → 3 → 4 → 5 → 6 → 7.
  • Return the head of the reversed list, which is 7 → 6 → 5 → 4 → 3 → 2 → 1.
  • Each node holds a value and a single next pointer to the node after it.
  • The last node points to 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.

3. Concepts You Need Here

3.1 What a Singly Linked List Looks Like

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.

  • The val field stores the data, such as a number.
  • The 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; }
}

3.2 Reversing Means Flipping Every Arrow

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.

  • Before: node 1 points to node 2, node 2 points to node 3, and so on.
  • After: node 2 points back to node 1, node 3 points back to node 2, and so on.

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.

4. Approach 1: Reverse Using a Stack

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.

4.1 Pseudocode

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 newHead

4.2 Pseudocode Explained

Think 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.

  • First we handle tiny lists. An empty list or a single node is already its own reverse, so we return it untouched.
  • Then the first loop pushes every node onto the stack. After it finishes, the original tail sits on top of the pile.
  • Next we pop the top node and call it the new head, because it used to be the tail.
  • The second loop keeps popping and links each node after the previous one, rebuilding the chain backward.
  • Finally we point the last node to null, so the new list has a clean end instead of a loop.

4.3 Java Code

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;
    }
}

4.4 Java Code Explained

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.

  • Lines 7 to 9 handle the tiny cases. A null head or a lone node returns right away.
  • Lines 10 to 11 create the stack and a walking pointer named node that starts at the head.
  • Then lines 12 to 15 push every node and march node forward until it falls off the end.
  • Line 16 pops the top node, the old tail, and names it newHead.
  • Lines 18 to 21 pop the rest and chain each one after tail, moving tail along each time.
  • Line 22 sets the final node’s next to null, which closes the list so it does not point back into itself.

4.5 Dry Run of the Stack Approach

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.

stepnode.val pushedstack (bottom → top)
01[1]
12[1, 2]
23[1, 2, 3]
34[1, 2, 3, 4]
45[1, 2, 3, 4, 5]
56[1, 2, 3, 4, 5, 6]
67[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.

steppopped nodeactionreversed list so far
set head7newHead = 7, tail = 77
16tail.next = 6, tail = 67 → 6
25tail.next = 5, tail = 57 → 6 → 5
34tail.next = 4, tail = 47 → 6 → 5 → 4
43tail.next = 3, tail = 37 → 6 → 5 → 4 → 3
52tail.next = 2, tail = 27 → 6 → 5 → 4 → 3 → 2
61tail.next = 1, tail = 17 → 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.

4.6 Reading the Dry Run

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.

  • At step 0 we push node 1, so the pile holds just [1].
  • Reading step 1 pushes node 2, and step 2 pushes node 3, growing the pile in order.
  • Moving through steps 3, 4, and 5 pushes nodes 4, 5, and 6 on top of each other.
  • By step 6 we push node 7, the original tail, and it lands on top of the whole pile.

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.

  • First we pop node 7 and store it in newHead, so the reversed list starts at 7.
  • We also point 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.

  • At step 1 we pop node 6 and set tail.next = 6, so the list becomes 7 → 6, and tail moves to 6.
  • During step 2 we pop node 5, link it after 6, and the list reads 7 → 6 → 5.
  • Then steps 3 and 4 pop nodes 4 and 3, extending the chain to 7 → 6 → 5 → 4 → 3.
  • Finally steps 5 and 6 pop nodes 2 and 1, so the full chain becomes 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.

4.7 Time and Space Cost

  • Time is O(n), because we walk the list once to push and once to pop.
  • Space is O(n), since the stack stores a reference to every node.

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.

5. Approach 2: Reverse Using Recursion

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.

5.1 Pseudocode

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 newHead

5.2 Pseudocode Explained

The 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.

  • The base case stops the dive. A null node or the last node is returned as the new head, since it ends up at the front.
  • The recursive call reverses everything after the current node, and hands back the new head of that reversed part.
  • Then node.next.next = node tells the next node to point back at the current node.
  • After that node.next = null removes the old forward link, which would otherwise create a loop.

5.3 Java Code

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;
    }
}

5.4 Java Code Explained

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.

  • Lines 4 to 6 are the base case. An empty list or a single node is already reversed, so it returns straight away.
  • Line 7 calls the method on head.next, which reverses the rest and returns that part’s head as newHead.
  • Line 8 runs head.next.next = head, so the node right after head now points back at head.
  • Then line 9 sets head.next = null, which breaks the old forward link and prevents a cycle.
  • Line 10 returns newHead, and that same head bubbles up unchanged through every call.

5.5 Dry Run of the Recursion Approach

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.

callnodenode.nextwhat happens
reverse(1)12not base, calls reverse(2)
reverse(2)23not base, calls reverse(3)
reverse(3)34not base, calls reverse(4)
reverse(4)45not base, calls reverse(5)
reverse(5)56not base, calls reverse(6)
reverse(6)67not base, calls reverse(7)
reverse(7)7nullbase 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 intorewire: node.next.next = nodethen: node.next = nulllist built so far
reverse(6)7.next = 66.next = null7 → 6
reverse(5)6.next = 55.next = null7 → 6 → 5
reverse(4)5.next = 44.next = null7 → 6 → 5 → 4
reverse(3)4.next = 33.next = null7 → 6 → 5 → 4 → 3
reverse(2)3.next = 22.next = null7 → 6 → 5 → 4 → 3 → 2
reverse(1)2.next = 11.next = null7 → 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.

5.6 Reading the Dry Run

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.

  • The call reverse(1) sees that node 1 has a next, so it pauses and calls reverse(2).
  • In the same way reverse(2) calls reverse(3), and the chain keeps diving one node at a time.
  • Each paused call waits on the shelf, remembering its own node until the deeper call returns.
  • At 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.

  • The line node.next.next = node becomes 7.next = 6, so node 7 now points to node 6.
  • Then 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.

  • Back in reverse(5) the rewire 6.next = 5 extends the chain to 7 → 6 → 5, and 5.next = null.
  • During reverse(4) the rewire 5.next = 4 gives 7 → 6 → 5 → 4, and 4.next = null.
  • Then 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.

  • The rewire 2.next = 1 attaches node 1 at the end, so the list becomes 7 → 6 → 5 → 4 → 3 → 2 → 1.
  • Setting 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.

5.7 Time and Space Cost

  • Time is O(n), because each node is visited once as the calls dive and unwind.
  • Space is O(n), since the call stack holds one frame per node until the base case returns.

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.

6. Approach 3: Reverse In Place With Three Pointers

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.

6.1 Pseudocode

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 head

6.2 Pseudocode Explained

Picture 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.

  • We start prev at null, because the new tail must point to null once it is flipped.
  • The pointer curr begins at the head, which is the first node we flip.
  • Inside the loop we first save curr.next into nextNode, so the rest of the list is not lost when we flip the arrow.
  • Then curr.next = prev reverses the current node’s link to point backward.
  • After that both prev and curr step forward by one, ready for the next node.
  • When curr reaches null, the loop ends, and prev is sitting on the last node, the new head.

6.3 Java Code

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;
    }
}

6.4 Java Code Explained

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.

  • Lines 4 to 5 set up the pointers. prev starts at null, and curr starts at the head.
  • Line 6 keeps the loop running while curr still points at a real node.
  • Line 7 saves curr.next into nextNode before anything is changed, which protects the rest of the list.
  • Then line 8 runs curr.next = prev, the single line that actually reverses the link.
  • Lines 9 to 10 slide prev and curr forward, so the next loop works on the next node.
  • Line 12 returns prev, which lands on the old tail after the loop, and that is the new head.

6.5 Dry Run of the Iterative Approach

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.

itercurrnextNode savedprev beforeflip: curr.next = prevprev after
012null1.next = null1
12312.next = 12
23423.next = 23
34534.next = 34
45645.next = 45
56756.next = 56
67null67.next = 67

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.

6.6 Reading the Dry Run

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.

  • First we save nextNode = curr.next, so nextNode points at node 2 and the rest of the list is safe.
  • Then curr.next = prev sets 1.next = null, which flips node 1’s arrow to point at nothing.
  • After that 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.

  • We save nextNode = curr.next, so nextNode points at node 3 before we change anything.
  • The line curr.next = prev sets 2.next = 1, so node 2 now points back to node 1.
  • Both pointers slide forward: 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.

  • At iteration 2 the flip 3.next = 2 grows the reversed chain to 3 → 2 → 1.
  • During iteration 3 the flip 4.next = 3 extends it to 4 → 3 → 2 → 1.
  • Then iterations 4 and 5 flip nodes 5 and 6, so the chain becomes 6 → 5 → 4 → 3 → 2 → 1.
  • Each step saves 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.

  • We save nextNode = curr.next, which is null, since node 7 was the original tail.
  • The flip 7.next = 6 points node 7 back at node 6, finishing the full chain.
  • Next 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.

6.7 Time and Space Cost

  • Time is O(n), because we touch each node exactly once in a single pass.
  • Space is O(1), since we only keep three pointers no matter how long the list is.

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.

7. Comparing the Three Approaches

All three return the same reversed list. They just pay different prices in memory and clarity.

ApproachTimeSpaceNote
StackO(n)O(n)Easy to picture, but stores every node
RecursionO(n)O(n)Short and elegant, uses the call stack
Three pointersO(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.

8. Common Mistakes and Edge Cases

A few small slips catch beginners on Reverse Linked List. Keep these in mind.

  • Flipping curr.next before saving the next node loses the rest of the list instantly.
  • Forgetting to set the old head’s next to null leaves a loop between the last two nodes.
  • Returning head instead of prev in the loop version hands back the old tail, pointing the wrong way.
  • An empty list, where head is null, should return null without error.
  • A single-node list is already reversed, so it should come back unchanged.

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.

9. FAQ’s on reverse linked list in java

Q: What is the most efficient way to reverse a linked list in Java?

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.

Q: Why do you save the next node before flipping the pointer?

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.

Q: What is the space cost of reversing a linked list recursively?

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.

Q: How do you reverse a linked list using a stack?

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.

Q: What should reverseList return for an empty or single-node list?

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.

10. Conclusion

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.

11. Further Reading

Leave a Comment