Reverse Linked List — Solution in Python, Java & C++
Reverse Linked List solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on recursion.
Reverse Linked List is an easy coding problem on recursion, asked in Amazon, Microsoft, Adobe and Apple interviews.
- Difficulty: Easy
- Topics: Recursion
- Asked at: Amazon, Microsoft, Adobe, Apple
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given the head of a singly linked list, reverse the list in place and return the head of the reversed list.
Example 1
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
Example 2
Input: head = [1,2]
Output: [2,1]
Example 3
Input: head = []
Output: []
Constraints
The number of nodes in the list is in the range [0, 5000]-5000 <= Node.val <= 5000
Follow-up: A linked list can be reversed either iteratively or recursively. Could you do both?
Hints for Reverse Linked List
Hint 1
Reversing means every
nextpointer should point at the node that used to come before it.Hint 2
Walk the list keeping two pointers:
prev(already reversed part) andcur(the rest).Hint 3
Save
cur.nextbefore overwriting it withprev, then shift both pointers one step. At the endprevis the new head.
How to solve Reverse Linked List
Turn each arrow around as you pass it. Keeping the already reversed prefix in prev and the untouched suffix in cur, one node moves from the suffix to the front of the prefix per step.
Approach
- Set
prev = null,cur = head. - While
curis not null: remembernext = cur.next; setcur.next = prev; thenprev = cur,cur = next. - Return
prev.
Why it works
Invariant: prev heads the reversal of the nodes visited so far and cur heads the nodes not yet visited, in their original order. Each step moves one node from cur to the front of prev, preserving it; when cur is empty, prev is the whole list reversed.
Complexity
- Time —
O(n) - Space —
O(1) iteratively (O(n) stack for the recursive version)
Pitfalls
- Overwriting
cur.nextbefore saving it loses the rest of the list. - Return
prev, nothead— the old head is now the tail. - The recursive version must set the old head's
nextto null, or the last two nodes form a cycle.
Reference solution
Python
from typing import Optional
def reverseList(head: Optional[ListNode]) -> Optional[ListNode]:
prev, cur = None, head
while cur:
nxt = cur.next
cur.next = prev
prev, cur = cur, nxt
return prevJava
public static ListNode reverseList(ListNode head) {
ListNode prev = null, cur = head;
while (cur != null) {
ListNode next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}C++
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* cur = head;
while (cur) {
ListNode* next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
return prev;
}JavaScript
var reverseList = function(head) {
var prev = null, cur = head;
while (cur !== null) {
var next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 40 recursion problems · the whole catalogue
Learn the technique: Recursion