Reverse Linked List Java | Fresher Interview
Fast recall
Quick revision
Core answer
next pointers in place using three references - prev, curr, next. For each node: save next, set curr.next = prev, then move prev and curr forward. The new head is prev when curr hits null. O(n) time, O(1) space. A recursive version is shorter but uses O(n) stack space.Whether you can write the in-place pointer-reversal and choose iterative (O(1) space) over recursive
Overwriting curr.next before saving the next node, or returning head instead of prev
Contrast iterative O(1) space with recursive O(n) stack space and return prev as the new head
Interview-ready guide
Interview answer
To reverse a singly linked list in Java, the approach I prefer is iterative and in place, using three references: prev, curr, and a temporary next.
Prev starts as null and curr starts at the head. I walk through the list, and for each node I do four steps in a careful order. First I save curr dot next into next, so I don't lose the rest of the list once I rewire the pointer.
Then I flip the link by setting curr dot next to prev. Then I advance prev to curr and curr to next. When curr becomes null, every node has been flipped, and prev is now pointing at the original last node, which is the new head, so I return prev.
This is one pass, O of n time, and O of one space because I'm only juggling a few pointers. I'd also mention there's a recursive version, which is quite elegant: you recurse to the end, then on the way back you set head dot next dot next to head and head dot next to null.
It's clean and shows good understanding, but it uses O of n stack space because of all the nested calls, and on a very long list it can even overflow the stack. So for an in-place, constant-space reversal I go with the iterative version.
The two mistakes I consciously avoid are flipping curr dot next before I've saved the next node, which strands the rest of the list, and returning the old head instead of prev, since the old head ends up as the tail.
For example, reversing 1 -> 2 -> 3 first makes 1 -> null, then 2 -> 1, and finally 3 -> 2 -> 1. At that point prev is node 3, so it becomes the new head.
Reverse Linked List Java | Fresher Interview
Direct Answer
Reverse the next pointers in place using three references - prev, curr, next. For each node: save next, set curr.next = prev, then move prev and curr forward. The new head is prev when curr hits null. O(n) time, O(1) space. A recursive version is shorter but uses O(n) stack space.
Deep Dive & Architecture
Iterative reversal keeps three references: prev is the already reversed prefix, current is the node being changed, and next saves the untouched remainder. The order of those assignments is the whole algorithm.
Worked example: 1 -> 2 -> 3 -> null
Initially prev is null and current is 1. Save node 2, point 1.next to null, then move prev to 1 and current to 2. The next round saves 3 and creates 2 -> 1. The final round creates 3 -> 2 -> 1; current becomes null and prev, node 3, is the new head.
The invariant is that prev always heads a correctly reversed list, while current heads the part not processed yet. Saving current.next before changing it prevents losing every remaining node.
An empty list returns null, and a one-node list returns that node without special code. Each node is visited once, so time is O(n) and iterative extra space is O(1). A recursive version is shorter but uses O(n) call-stack space and can overflow the stack on a long list.
The operation changes the original links. If callers need the old list unchanged, create new nodes instead and state the O(n) space cost.
Common Mistakes & Pitfalls
- Flip before save: setting
curr.next = prevbefore storingnextloses the tail. - Return head: return prev (the old head is now the tail).
- Recursive on long lists: O(n) stack can overflow; prefer iterative.
- Forgetting null start:
prevbegins null so the old head ends at null.
Check your understanding
Answer these without looking back at the article.
- How do you detect a cycle in a linked list?
- How do you find the middle element of a linked list?
- What is a linked list and how does it differ from an array?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.