14. Reverse Linked List
easyAsked at MonzoReverse an audit-trail linked list so the most recent ledger entry comes first.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the head of a singly linked list, reverse the list, and return the reversed list. Solve it both iteratively and recursively if asked.
Constraints
Number of nodes in the list is in the range [0, 5000]-5000 <= Node.val <= 5000
Examples
Example 1
head = [1,2,3,4,5][5,4,3,2,1]Example 2
head = [1,2][2,1]Approaches
1. Recursive
Recurse to the tail, then rewire next pointers on the way back.
- Time
- O(n)
- Space
- O(n)
function reverse(node) {
if (!node || !node.next) return node;
const newHead = reverse(node.next);
node.next.next = node;
node.next = null;
return newHead;
}Tradeoff:
2. Iterative pointer flip
Walk the list with prev/cur pointers and flip the next reference at each step. Constant stack.
- Time
- O(n)
- Space
- O(1)
function reverseList(head) {
let prev = null, cur = head;
while (cur) {
const nxt = cur.next;
cur.next = prev;
prev = cur;
cur = nxt;
}
return prev;
}Tradeoff:
Monzo-specific tips
Monzo expects you to discuss both versions and call out the stack-depth risk on long audit chains.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Reverse Linked List and other Monzo interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →