Quick revision
The short answer
Iteratively, we walk the list with two references, prev starting at null and cur at the head, and for each node we save cur.next, point cur.next back to prev, and move both forward; at the end prev is the new head. Recursively, we reverse the rest of the list first, then make the next node point back with head.next.next = head, set head.next = null, and return the new head from the deepest call. Both are O(n) time, but the iterative version uses O(1) space while the recursive one uses one stack frame per node, and since Java has no tail call optimization, 1 million nodes will throw StackOverflowError with default stack sizes, so production code uses the iterative version.
What to remember
In this answer, we reversed a singly linked list two ways. The iterative version saves next, points the current node back to prev and moves both forward, giving O(n) time and O(1) space. The recursive version reverses the rest first and then fixes one link with head.next.next = head and head.next = null, which is also O(n) time but needs a stack frame per node. Because Java has no tail call optimization, a million-node list overflows the default stack, so the iterative version is the one we use in production.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Iteratively, we walk the list with two references, prev starting at null and cur at the head, and for each node we save cur.next, point cur.next back to prev, and move both forward; at the end prev is the new head. Recursively, we reverse the rest of the list first, then make the next node point back with head.next.next = head, set head.next = null, and return the new head from the deepest call. Both are O(n) time, but the iterative version uses O(1) space while the recursive one uses one stack frame per node, and since Java has no tail call optimization, 1 million nodes will throw StackOverflowError with default stack sizes, so production code uses the iterative version.
Why this question is asked
Reversing a linked list is the classic pointer exercise: it is only a few lines, but it is easy to lose the rest of the list by overwriting a reference too early. Interviewers ask for both versions to check two things: that we can manage references carefully, and that we understand what recursion costs at run time in Java. The second part, the million-node list, is about the call stack, which matters in real code that walks deep structures such as trees, nested JSON or long chains of objects. In this answer, we will recap the list structure, write the iterative version and trace it, write the recursive version and see how it works, explain why recursion overflows on a long list, and then reverse only part of a list.
1A Quick Recap of the List and the Goal
We have class Node { int val; Node next; }. Each node points to the next one, and the last node points to null. For 1 -> 2 -> 3 -> null, reversing means the result is 3 -> 2 -> 1 -> null, and we return node 3 as the new head. We want to do it in place, by changing the next references of the existing nodes, not by creating new nodes.
The difficulty is that a singly linked list only lets us move forward. As soon as we point a node's next backwards, we lose our only way to reach the rest of the list, unless we saved it first.
2The Iterative Version
We keep three references: prev, the already reversed part; cur, the node we are working on; and next, a temporary copy of cur.next so we do not lose the rest of the list.
- Save next = cur.nextkeep the rest of the list reachable
- Set cur.next = prevthis node now points backwards
- Move prev = curthe reversed part grows by one
- Move cur = nextcontinue with the saved rest
- cur == null?YesReturn prev as the new headNoRepeat the loop
5 steps in order. Play them, or step through them one at a time.
Notice the order of the four lines. If we wrote cur.next = prev before saving next, the reference to node 2 would be gone and the loop would stop after one node. Saving first is the whole trick. Each node is visited once, so the time is O(n), and we only use three references, so the extra space is O(1). An empty list returns null and a one-node list returns that node, without special cases.
3The Recursive Version
The recursive version trusts the function to reverse the rest of the list, then fixes the link between the current node and the rest.
For 1 -> 2 -> 3, the calls go down to node 3, which returns itself as the new head. Back in the call for node 2, head.next is node 3, so head.next.next = head makes 3 point to 2, and head.next = null cuts 2's old forward link. Back in the call for node 1, the same two lines make 2 point to 1 and make 1 the tail. Every level returns the same newHead, node 3.
It is elegant, and it is O(n) time. But each call waits for the deeper call to finish, so at the deepest point there is one stack frame per node: O(n) space on the call stack.
4Why Recursion Fails on 1 Million Nodes
Each thread in Java has a fixed-size call stack, set by -Xss or a thread constructor argument. The default on common 64-bit platforms is around 1 MB, and the exact value depends on the platform and JVM, so check yours. Every recursive call needs a frame for its parameters, local variables and return address. Even a small frame of a few dozen bytes means 1 MB runs out after some tens of thousands of nested calls, far short of a million.
Some languages remove this cost with tail call optimization: when the recursive call is the last thing a function does, the compiler reuses the frame. Java does not do this, neither javac nor the JIT, and our recursive call is not even a tail call, because we still have work to do after it returns. So reverseRecursive on a million-node list throws StackOverflowError, and the iterative version handles it in a few milliseconds with constant memory.
We could give a thread a bigger stack, but that only moves the limit and costs memory for every thread that uses it. For production, the answer is the iterative version. The recursive one is still worth knowing, because it shows we can think recursively, and it is fine for short lists.
5Follow-up questions
How would you reverse only nodes m to n?
I use a dummy node in front of the head so the case m = 1 needs no special handling. I walk to the node just before position m, call it before. Then I reverse the next n - m + 1 nodes with the same iterative loop, keeping a reference to the first node of that section, which becomes its tail. Finally I reconnect: before.next points to the new section head, and the old section head's next points to the node after position n. It is one pass, O(n) time and O(1) space, and I return dummy.next.
Does Java's LinkedList have a reverse method?
Since Java 21, LinkedList implements SequencedCollection, whose reversed() returns a reverse-ordered view rather than changing the list. Collections.reverse(list) reverses in place. For our own Node class there is nothing built in, which is why we write the loop.
Can you make the recursive version safe for long lists?
Not in Java without changing its shape, since there is no tail call optimization. We could run it in a thread created with a large stack size, but that is a workaround that costs memory and still has a limit. Converting it into a loop, which is exactly the iterative version, is the real fix.
6Conclusion
In this answer, we reversed a singly linked list two ways. The iterative version saves next, points the current node back to prev and moves both forward, giving O(n) time and O(1) space. The recursive version reverses the rest first and then fixes one link with head.next.next = head and head.next = null, which is also O(n) time but needs a stack frame per node. Because Java has no tail call optimization, a million-node list overflows the default stack, so the iterative version is the one we use in production.
Open with
Iteratively, I keep two references, prev starting at null and cur starting at the head. In each step I first save cur.next in a local variable, then point cur.next back at prev, then move prev to cur and cur to the saved next.
Then, if they want more
When cur becomes null, prev is the old tail, which is the new head, so I return it. Saving next first is the important part, otherwise I lose the rest of the list. Recursively, the base case is an empty list or a single node, which I return as the new head.
Otherwise I reverse the rest of the list from head.next, then set head.next.next to head so the next node points back to me, set head.next to null so I become the tail, and return the new head that came back from the deeper call. Both are O(n) time. The difference is space. The iterative one uses three references, O(1).
The recursive one keeps a stack frame for every node until the deepest call returns, so it is O(n) on the call stack. Java does not do tail call optimization, and this recursion is not a tail call anyway, so with a default thread stack of around a megabyte, a million nodes will throw StackOverflowError.
So in production I would use the iterative version, and I would only use the recursive one where the input is known to be short.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a Java method that reverses a singly linked list, given": https://scrivio-interview-library.netlify.app/java/coding-problems/5db9921788/