Reverse Linked List solution explained
To reverse a singly linked list, walk it once with three references: the node you are on, the node before it, and the node after it. Point the current node back at the previous one, step all three forward, and when you fall off the end the previous reference is the new head. It takes O(n) time and O(1) extra space.
By Evin JohnPublished 8 min read
The problem
A singly linked list is a chain of nodes, each holding a value and a reference to the next node, with the last node pointing at nothing. You receive a reference to the first node, or nothing for an empty list. Turn the chain around so that the old last node comes first and every link points the other way, then return the new first node. The nodes themselves should be reused, not copied.
| Before | After | Comment |
|---|---|---|
| 3 → 8 → 1 → 6 | 6 → 1 → 8 → 3 | The ordinary case |
| 9 → 4 | 4 → 9 | The smallest case where links actually change |
| 7 | 7 | One node points at nothing before and after |
| (empty) | (empty) | Return nothing |
Worth asking: should the reversal happen in place, or may you build a new list? Is recursion acceptable, given the list could be long? Is it singly or doubly linked? For a doubly linked list you swap each node's two references instead, which is a different loop.
Why it is asked so often
Reversing a list is the smallest problem that checks whether you can change references without losing part of the structure. Overwrite one link too early and the rest of the list becomes unreachable. Interviewers watch the order of your assignments, how you handle the empty list, and whether you can explain the state of the three references at every step. It is also a building block: several harder list problems are this loop plus some bookkeeping.
Likely follow-ups
- Write it recursively. Reverse everything after the head, then make the old second node point back at the head and cut the head's own link. Mention the call stack cost.
- Reverse only positions a through b. Walk to the node before position a, reverse exactly b - a + 1 links, then stitch both ends back on. A dummy node in front handles a = 1 cleanly. Code is below.
- Reverse in groups of k. Reverse each block of k nodes and connect it to the next block; leave a final short block alone or reverse it, depending on the version.
- Does the list read the same forward and backward? Find the middle with a slow and a fast pointer, reverse the second half, compare, and optionally restore it.
- Print the list backward without changing it. Use recursion or an explicit stack, which costs O(n) memory but leaves the links untouched.
Simple approach: copy the values out and back
Before the pointer version, it is fair to mention the obvious one. Walk the list and collect its values in an array, then walk it again and write the values back from the end of the array. The links never change, only the values do. It is O(n) time and O(n) extra memory, and it dodges the point of the question, so interviewers usually ask for the in-place version next.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def reverse_by_copy(head):
stash = []
node = head
while node:
stash.append(node.value)
node = node.next
node = head
while node:
node.value = stash.pop()
node = node.next
return headOptimal approach: flip the links in one pass
Keep three references. prev starts as nothing, because the old head will become the new tail and must point at nothing. current starts at the head. At each step, first save current.next in a temporary, because you are about to overwrite it. Then point current.next back at prev. Then move prev up to current and current up to the saved node. When current becomes nothing, every link has been flipped and prev sits on the old tail, which is the new head.
The order of those four assignments is the whole problem. Save, flip, advance prev, advance current. If you flip before saving, the remainder of the list is lost.
def reverse_list(head):
prev = None
current = head
while current:
upcoming = current.next # save the rest before overwriting
current.next = prev # flip this link
prev = current
current = upcoming
return prevstruct Node {
int value;
Node* next;
};
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* current = head;
while (current != nullptr) {
Node* upcoming = current->next;
current->next = prev;
prev = current;
current = upcoming;
}
return prev;
}def reverse_list_recursive(head):
if head is None or head.next is None:
return head
new_head = reverse_list_recursive(head.next)
head.next.next = head # the old second node now points back
head.next = None
return new_headPython's default recursion limit is about a thousand frames, so the recursive version raises an error on a list of a few thousand nodes unless the limit is raised. In C++ or Java, deep recursion risks a stack overflow. That is a good reason to present the loop as your main answer and the recursion as an alternative.
def reverse_between(head, a, b):
front = Node(0, head) # dummy so that a = 1 needs no special case
before = front
for _ in range(a - 1):
before = before.next
start = before.next # becomes the end of the reversed part
prev, current = None, start
for _ in range(b - a + 1):
upcoming = current.next
current.next = prev
prev = current
current = upcoming
before.next = prev # attach the reversed part
start.next = current # and the rest of the list after it
return front.nextComplexity
| Approach | Time | Extra space | Changes links? |
|---|---|---|---|
| Copy values out and back | O(n) | O(n) | No, only values |
| Iterative pointer reversal | O(n) | O(1) | Yes, the expected answer |
| Recursive reversal | O(n) | O(n) call stack | Yes |
| Reverse positions a to b | O(b) | O(1) | Yes, only inside the range |
Edge cases
- Empty list: return nothing without touching any field.
- One node: it is its own reversal, and its next must stay empty.
- Two nodes: the first real flip, and a quick test of the assignment order.
- A long list with the recursive version: mention the recursion limit or stack depth.
- For the partial follow-up: a range that starts at the head, ends at the tail, or covers a single position.
Mistakes to avoid
- Overwriting current.next before saving it, which strands every node after the current one.
- Returning head at the end. After the loop, head is the tail; the answer is prev.
- Forgetting to set the old head's next to nothing in the recursive version, which leaves a two-node cycle at the end.
- Starting prev at the head instead of at nothing, which also creates a cycle.
- Building a new list of new nodes when the interviewer asked for an in-place reversal.
How to explain it out loud
Fictional example
- Candidate
- I keep prev, which starts empty, and current, which starts at the head. In the loop I save current's next first, so I do not lose the rest of the list.
- Candidate
- Then I point current back at prev and move both forward. When current runs off the end, prev is the old tail, so I return it. That is one pass and constant extra space.
- Candidate
- For 3, 8, 1, 6: after the first step 3 points at nothing, then 8 points at 3, 1 at 8, and 6 at 1, so 6 is the new head.
Other list problems in this set
Merge Two Sorted Lists uses the same dummy-node trick as the partial reversal above to splice two chains together, and Linked List Cycle introduces the slow and fast pointers you need for the palindrome follow-up. The same care with link updates carries over to design questions such as LRU Cache, where a doubly linked list has to be rewired on every access. Browse everything on the coding problems hub.
Rehearsing pointer problems with Natively
Pointer bugs are easiest to catch when you say each assignment aloud. Natively runs on Mac and Windows, and during practice it can read a list problem from a screenshot and suggest an approach and code, so you can compare the order of assignments with yours. It transcribes your mock interview as well, which lets you check afterward whether you explained what prev and current hold at each step. See the AI interview assistant.
Questions people ask
How do you reverse a linked list iteratively?
Keep prev, starting empty, and current, starting at the head. Each step, save current's next, point current back at prev, then move prev and current forward. When current runs out, prev is the new head. O(n) time, O(1) space.
What is the space complexity of reversing a linked list recursively?
O(n), because each node adds a frame to the call stack. On long lists that can hit a recursion limit or overflow the stack, which is why the loop is usually preferred.
What is the most common bug when reversing a linked list?
Overwriting current's next before saving it, which loses the rest of the list. Returning the old head instead of prev at the end is a close second.
How do you reverse only part of a linked list?
Walk to the node before the range, reverse exactly the nodes in the range with the same loop, then connect the node before the range to the new front and the old front to the node after the range. A dummy node handles a range that starts at the head.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
