Quick revision
The short answer
Reversing a singly linked list is a linked list problem solved by in-place pointer rewiring. We walk the list once with three references, prev, curr and next, save the rest of the list before cutting each link, turn each node's arrow back towards the node before it, and return prev, the old last node. It takes O(n) time and O(1) extra space.
The solution
What it costs
| Approach | Time | Extra space | Reverses the links? |
|---|---|---|---|
| Copy values out and write them back | O(n) | O(n) | no, it moves values |
| Push nodes on a stack, then relink | O(n) | O(n) | yes |
| Recursive reversal | O(n) | O(n) call stack | yes |
| Loop with prev, curr and next | O(n) | O(1) | yes |
What to remember
We reversed a singly linked list by walking it once with three references: next keeps the rest within reach, curr.next = prev turns one arrow back, and moving prev and curr along grows the reversed part until prev is the new head. It takes O(n) time and O(1) space. The clue to remember is a singly linked list whose shape must change: ask which nodes you would lose sight of when a link changes, keep a reference on each, and rewire in one pass.
1What it means to turn a linked list around
Think of a goods train standing on a track. Each coach carries some cargo and has one coupling that holds the coach behind it, so to reach the fifth coach you must walk along from the first. A singly linked list is exactly this train. Each coach is a node, its cargo is the node's value, and its coupling is a reference called next, a variable that says which node comes after it. The first node, called the head, is the only one we are handed, and the last node's next refers to nothing, which Java writes as null.
Reversing the list means turning every coupling around, so the train can be entered from the other end. Notice how small that job is. No node moves and no value changes; each of the five next references changes exactly once. The problem feels frightening only because changing a reference can make part of the list vanish from view, and we will see exactly how to prevent that.
Let us keep one example for the whole article. We are given 1 -> 2 -> 3 -> 4 -> 5 and must hand back 5 -> 4 -> 3 -> 2 -> 1, entered at node 5. Node 1 pointed to node 2 and must now point to null, because it becomes the last node. Node 2 will point to node 1, node 3 to node 2, and so on, and node 5, which pointed to null, will point to node 4.
Now do it once on paper. Draw five boxes with an arrow from each to the next. Going from the left, rub out each box's arrow and draw a new one to the box on its left, writing null for the first box. Notice that every new arrow goes to the box you stood on a moment ago. Keep that observation in mind, because it will become the whole solution.
Top row: the list we are given. Bottom row: the list we must hand back, made of the same five nodes.
1The top row is the input, 1 -> 2 -> 3 -> 4 -> 5, entered at node 1. The bottom row is the answer, the same nodes linked the other way and entered at node 5.
What to assume
- Do we change the links or just swap the values?
- The links, because real nodes often carry far more than one number and other code may hold references to them.
- Can the list be empty or have one node?
- Yes, and both come back unchanged.
- Can it contain a loop?
- We assume not, because the walk would never end. And the caller must use the returned head, as in head =
reverse(head), since its old variable still refers to node 1.
A careful candidate asks a few questions first. Do we change the links or just swap the values? The links, because real nodes often carry far more than one number and other code may hold references to them. Can the list be empty or have one node? Yes, and both come back unchanged. Can it contain a loop? We assume not, because the walk would never end. And the caller must use the returned head, as in head = reverse(head), since its old variable still refers to node 1.
Your turn
Solve it before you read on. Above is one example with the result expected for it. Under What to assume are the points this solution takes as given. Say your approach aloud or type it below, and write the code in your own editor. Then take one hint at a time and compare.
2How to recognise a pointer rewiring problem
Most of us first reverse things in arrays, with one index at each end, a swap, and both indexes moving towards the middle. A beginner naturally tries the same here, but a linked list cannot jump to its last node. Reaching node 5 takes four steps from node 1, and reaching node 4 afterwards means starting from node 1 again, because no node knows the one before it. So the array habit costs a fresh walk for every swap. The shape of the data decides the method.
So let us read the question slowly and take its clues one at a time. The first clue is the word singly. Each node knows only the node after it, so we work while walking forward, and a node we have walked past is out of reach unless some variable still refers to it. The real work is deciding which nodes to keep hold of.
The second clue is the word reverses. We are asked to change the shape of the list, which node follows which, and in a linked list the shape lives entirely in the next references. The question also offers no second list, and interviewers expect the answer to work in place, reusing the nodes we already have.
Together the clues point to the linked lists family, and to a technique called in-place pointer rewiring. In place means we reuse the existing nodes and build no copy. A pointer, in Java, is a reference variable that names a node, like a finger resting on one coach. Rewiring means changing what a node's next refers to. The rule to carry to new problems is to ask, before changing any next reference, which nodes you will lose sight of, and to keep a variable on each. For reversal, three are enough.
Some patterns look similar but do not fit. Fast and slow pointers, two references walking at different speeds, find the middle of a list or a loop, but they change no links. A stack, which works like a pile of plates where the last plate on is the first off, does reverse the order, but it stores every node again, so it is really a form of the brute force.
3The honest first attempt, copying the values and writing them back
When a problem feels unfamiliar, a good first move is to turn it into one we already know. We can reverse values in an ArrayList, because it can be read from either end. So the honest first attempt avoids pointers: copy every value into an ArrayList, then walk the list again and write the values back, last one first. It is correct and easy to check, which makes it a good start.
Let us do it by hand. The first walk collects [1, 2, 3, 4, 5]. The second walk writes 5 into the first node, 4 into the second, and so on, so the list now reads 5, 4, 3, 2, 1. But notice what did not change: every arrow is exactly where it was. The first node is still the head; it simply holds a different value.
Now let us count. Each loop visits five nodes, ten visits in all, and the ArrayList holds five numbers. For n nodes that is 2n visits and n stored values. When the work grows in step with n, we write the time as O(n), read as order n, and the extra memory, called the space, is O(n) too.
So where is the waste? Not in the time, because every node must be visited anyway. It is in memory, since we keep a whole copy only to read it backwards once, and in the kind of work, since we moved values instead of links. Code holding a reference to the node with 5, perhaps a customer record, now sees node 1's data. That is why interviewers ask for the links to be reversed.
So let us ask the question that leads further. When we stood on each node during the first walk, did we already know where its new arrow should point? We did: to the node we had just left, as the paper exercise showed. The copy threw that knowledge away.
4Getting to the in-place reversal, one observation at a time
While we walk forward, the node we have just left is where the current node's arrow must point. So we keep a finger on it, a variable called prev, and a second finger, curr, on the node we stand on. curr starts on the head, node 1. There is no node before node 1, so prev starts as null, which is exactly right, because node 1 must end up pointing to null as the new last node.
Now comes the question every beginner asks. Why not simply write curr.next = prev and move curr on? Let us try it and see what we lose. curr is node 1 and prev is null, so node 1 now points to null. Next we want curr on node 2, but the only route to node 2 was node 1's next, which we have just overwritten. Nodes 2 to 5 still exist in memory, yet no variable refers to them, so for our program they are gone. It is the train again: uncouple the first coach before tying a rope to the second, and the rest of the train rolls away.
The middle frame is the mistake. The last frame is what the real loop does, with next saved first.
- prev
- null
- curr
- node 1
1At the start, curr is on node 1 and prev is null. The only way to reach node 2 is the arrow that leaves node 1.
- prev
- null
- curr
- node 1
- next
- never saved
2Suppose we write
curr.next= prev straight away. Node 1 now points to null, and no variable refers to node 2, so nodes 2 to 5 are lost to us.- prev
- null
- curr
- node 1
- next
- node 2
3The real loop first runs next =
curr.next. Now the same cut is harmless, because next still holds node 2 and the rest of the list behind it.
The cure is a third finger. Before cutting the link, we hold the rest of the list with next = curr.next. One step of the walk is then four assignments in a fixed order: next = curr.next saves the way forward, curr.next = prev turns the arrow back, prev = curr steps prev forward, and curr = next steps curr forward. Read them as one sentence: remember the way on, turn the arrow back, move both fingers along.
Does the order of the last two lines matter as well? It does. If curr = next came first, curr would already be on node 2, and prev = curr would put prev on node 2 too, so we would lose our hold on node 1. prev must step onto the node curr is leaving before curr leaves it.
Why does this always work? At the top of every step the list is split into two chains: the reversed part, which starts at prev, and the untouched rest, which starts at curr. At first the reversed part is empty and the rest is the whole list. Each step moves one node from the front of the rest to the front of the reversed part, so the description stays true. After n steps curr is null, every node is in the reversed part, and prev is its first node, the old last node. A tempting slip is to return head here, but head still refers to node 1, now the last node, so the answer is always prev.
5Watching the arrows turn on the five-node example
The picture plays the loop on 1 -> 2 -> 3 -> 4 -> 5. The nodes never move; only their arrows change. An arrow pointing right is untouched, an arrow pointing left has been turned, and the short cut mark means next is null. Under the nodes are our three fingers, and the panel shows what each one holds.
The first step is shown slowly, one or two lines of code per frame, so you can see the order of the four assignments. After that each frame shows a whole step. The nodes marked as done form the reversed part.
The nodes never move. Watch the arrows turn from right to left and the three references step along under the nodes.
- prev
- null
- curr
- node 1
- next
- not set yet
1We begin with prev at null, because nothing is reversed yet, and curr on the head, node 1. Every arrow still points to the right.
- prev
- null
- curr
- node 1
- next
- node 2
2Step 1 begins with next =
curr.next, which puts a finger on node 2, so the rest of the list stays within reach before we change anything.- prev
- null
- curr
- node 1
- next
- node 2
3Then
curr.next= prev turns node 1 around, so it now points to null, shown by the cut mark. Nodes 2 to 5 are still reachable because next holds node 2.- prev
- node 1
- curr
- node 2
- next
- node 2
4The last two lines step prev onto node 1 and curr steps onto node 2. The reversed part is node 1 alone, and the rest starts at node 2.
- prev
- node 2
- curr
- node 3
- next
- node 3
5Step 2 does the same four lines: save node 3, point node 2 back at node 1, then step prev onto 2 and curr onto 3. The reversed part is 2 -> 1.
- prev
- node 3
- curr
- node 4
- next
- node 4
6Step 3 turns node 3 back towards node 2. The reversed part is now 3 -> 2 -> 1, and the untouched rest is 4 -> 5.
- prev
- node 4
- curr
- node 5
- next
- node 5
7Step 4 turns node 4 back towards node 3. Only node 5 is left in the rest, and curr and next are both on it.
- prev
- node 5
- curr
- null
- next
- null
8Step 5 saves next as null, points node 5 back at node 4, and moves curr to null. Every arrow now points to the left.
- prev
- node 5
- curr
- null
- next
- null
9The loop test curr != null fails, so the loop ends. prev is on node 5, the old tail, and we return it as the new head: 5 -> 4 -> 3 -> 2 -> 1.
Let us read the first step back. In the second frame only next has moved, onto node 2. In the third, node 1's arrow is cut to null, the moment that would have lost the list without the save, but next still holds node 2. In the fourth, prev moves onto node 1 and curr onto node 2.
In each later frame one more arrow turns and the reversed part grows by one node, which is the two chains idea made visible. Check one yourself: after step 3, following the arrows from prev gives 3 -> 2 -> 1, and from curr gives 4 -> 5.
Step 5 has a moment that worries beginners. On node 5, next = curr.next sets next to null. That is harmless, because we only copy it into curr, and that is exactly what makes the loop test curr != null fail. prev is on node 5, and we return it.
6The complete Java program
Here is the full program, with a small Node class, the reverse method, and two helpers, build to make a list and show to print one. main reverses our five-node list, a single node and an empty list, and each comment shows what that line prints.
Let us read reverse in the order it runs. prev starts as null, because nothing is reversed yet and the old head must end up pointing to nothing. curr starts on head, the front of the untouched rest. The while loop runs as long as some node is still waiting to be turned, so once per node.
Inside the loop the four lines run in the order we worked out. We save curr.next in next first, because the very next line overwrites it and it is our only route onward. Then curr.next = prev turns the arrow back, prev = curr makes this node the front of the reversed part, and curr = next moves us onto the rest we saved.
When the loop ends, curr is null and prev is the old last node, so return prev hands back the new head. Returning head instead would print [1] for our example.
The helpers only make the program runnable. build puts a spare dummy node in front so that the first real node needs no special case, and show prints the values with arrows. So main prints [5 -> 4 -> 3 -> 2 -> 1], then [7], then [].
7How much it costs, and the cases at the edges
Let us count on the example first. The loop ran five times, once per node, with four assignments each, twenty in all. For n nodes that is about 4n assignments, so the time is O(n). We cannot do better, because every one of the n arrows must change at least once.
Whether the list has five nodes or five million, we use the same three references, so the extra space is O(1), read as constant. The copy stored n values, and a recursive version keeps one waiting method call per node, which is O(n) stack space and can throw a StackOverflowError on a list of a few hundred thousand nodes.
| Approach | Time | Extra space | Reverses the links? |
|---|---|---|---|
| Copy values out and write them back | O(n) | O(n) | no, it moves values |
| Push nodes on a stack, then relink | O(n) | O(n) | yes |
| Recursive reversal | O(n) | O(n) call stack | yes |
| Loop with prev, curr and next | O(n) | O(1) | yes |
The edge cases need no special code. For an empty list curr starts as null, the loop never runs, and we return prev, which is null, the correct answer. For a single node the loop runs once, sets the node's next to null, which it already was, and returns the node, so main prints [7].
A two-node list is a good test of the order of the lines, so trace it by hand. Start with 1 -> 2. Step 1 saves node 2, points node 1 at null, and moves prev to 1 and curr to 2. Step 2 saves null, points node 2 at node 1, and moves prev to 2 and curr to null. We return node 2, and the list reads 2 -> 1.
8Follow-up questions
How would you write the recursive version, and why is it not the default?
If head is null or head.next is null, the list is already reversed, so we return head. Otherwise newHead = reverse(head.next) reverses everything after head. At that point head.next still refers to the node that followed head, which is now the last node of the reversed part, so head.next.next = head hangs head on the end, head.next = null ends the list there, and we return newHead. It is short and correct, but every node adds a method call that waits on the stack, so it uses O(n) space and can overflow on a long list.
How would you reverse only the part of the list between positions m and n?
We walk to the node just before position m and call it before. Then we run the same loop for n - m + 1 steps, starting with curr at position m. When it stops, prev is the front of the reversed slice and curr is the node after it. The node that was at position m is now the slice's last node, so we point it at curr, and we point before at prev. A dummy node in front of the head removes the special case when m is 1. It stays O(n) time and O(1) space.
Could you use a stack instead?
Yes. We walk the list and push every node onto a stack, our pile of plates, so node 5 ends up on top. Then we pop the nodes one by one, linking each to the next one popped, and set the last node's next to null. The first node popped, node 5, is the new head. It works, but the pile holds every node, so it needs O(n) extra space.
9Conclusion
We reversed a singly linked list by walking it once with three references: next keeps the rest within reach, curr.next = prev turns one arrow back, and moving prev and curr along grows the reversed part until prev is the new head. It takes O(n) time and O(1) space. The clue to remember is a singly linked list whose shape must change: ask which nodes you would lose sight of when a link changes, keep a reference on each, and rewire in one pass.
Open with
Let me confirm the requirements first: I reverse the links rather than the values, I return the new head, and the list may be empty or have one node.
Then, if they want more
Copying the values into an ArrayList and writing them back reversed works, but it costs O(n) extra memory and moves values instead of links. Since this changes the shape of a singly linked list, I will rewire the links in one pass. I keep prev, starting at null, and curr, starting at the head.
In each step I first save next as curr.next, because changing curr.next would otherwise lose the rest of the list. Then I set curr.next to prev, move prev to curr, and move curr to next. The invariant is that prev heads the reversed part and curr heads the untouched rest, and each step moves one node across.
When curr is null, prev is the old tail, so I return prev, not head. That is O(n) time and O(1) space. An empty list returns null and a single node comes back unchanged.
About this answer
- Last updated
- 15 November 2026
- Written for
- Java 21, using only java.util
- Checked
- The Java solution was compiled and run, and each printed result matches the comment beside it. The pictures are checked against the format when the site is built. The explanation around the code is a study note: confirm anything critical against the documentation below. What the checks cover
- Where the question comes from
- Public interview write-ups, reworded by Scrivio; counted 156 times in that collection. How it is counted
- Official documentation
- StringBuilder (Java 21), java.util, package documentation
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 code that reverses a singly linked list.": https://scrivio-interview-library.netlify.app/data-structures/solutions/write-code-that-reverses-a-singly-linked-list-13ef56/
Coding patterns, question 7 of 15 Back to the path
More linked lists questions