Loading…
Reverse Linked List is a easy coding interview question covering linked list, recursion — the kind asked at FAANG (Facebook/Meta, Apple, Amazon, Netflix, Google) and AI-first companies like Anthropic, OpenAI, and Nvidia.
Reverse a singly linked list and return the new head.
Example: 1 -> 2 -> 3 -> 4 -> 5 becomes 5 -> 4 -> 3 -> 2 -> 1
Implement both iterative and recursive solutions.
Try it yourself first
In an interview you'd get about 15 minutes for a easy question. Sketch an approach before peeking — struggling is where the learning happens.
Stuck? Reveal them one at a time — each hint gives away a little more.
Ready to compare notes? The full solution, visualization, and complexity analysis are below.
Iterative: walk the list, reversing each next pointer. Three-pointer technique: prev, curr, next_node. Recursive: reverse the tail, then make the former second node point back to the head.
| 1 | class ListNode: |
| 2 | def __init__(self, val=0, next=None): |
| 3 | self.val = val |
| 4 | self.next = next |
| 5 | |
| 6 | # Iterative — O(1) space |
| ▶ | def reverse_iterative(head: ListNode) -> ListNode: |
| ▶ | prev, curr = None, head |
| 9 | while curr: |
| 10 | nxt = curr.next |
| 11 | curr.next = prev |
| 12 | prev = curr |
| 13 | curr = nxt |
| 14 | return prev |
| 15 | |
| 16 | # Recursive — O(n) stack |
| 17 | def reverse_recursive(head: ListNode) -> ListNode: |
| 18 | if head is None or head.next is None: |
| 19 | return head |
| 20 | new_head = reverse_recursive(head.next) |
| 21 | head.next.next = head |
| 22 | head.next = None |
| 23 | return new_head |
Init: prev=None, curr=1.
Key Insights
A full interview loop is more than one round. These are the others, each with what is on the site for it.