17. Reverse Linked List
easyAsked at AdyenGiven the head of a singly linked list, reverse the list and return the new head.
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's head.
Constraints
0 <= list length <= 5000-5000 <= Node.val <= 5000
Examples
Example 1
head = [1,2,3,4,5][5,4,3,2,1]Example 2
head = [][]Approaches
1. Recursive
Recurse to tail, then rewire pointers on the way back.
- Time
- O(n)
- Space
- O(n)
function reverse(head) {
if (!head || !head.next) return head;
const tail = reverse(head.next);
head.next.next = head;
head.next = null;
return tail;
}Tradeoff:
2. Iterative pointer flip
Walk forward and reverse next pointers in place with three pointers.
- Time
- O(n)
- Space
- O(1)
function reverseList(head) {
let prev = null, cur = head;
while (cur) {
const next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}Tradeoff:
Adyen-specific tips
Adyen expects the iterative O(1)-space variant first — they avoid recursion in payment-flow code so a stack-blowing 5000-node list is exactly the constraint they probe.
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 Adyen interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →