Reverse Linked List
EasyPrompt
Given the head of a singly linked list, reverse the list, and return 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 the range
[0, 5000]. -5000 <= Node.val <= 5000
Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?
Approaches
2 approaches with complexity analysis and trade-offs.
This approach uses recursion to reverse the linked list by recursively reaching the end of the list and then backtracking to reverse the links.
Algorithm
- Base case: if head is null or head.next is null, return head
- Recursively call reverseList on head.next until reaching the end
- For each recursive call during backtracking:
- Set head.next.next = head (reverse the link)
- Set head.next = null
- Return the new head of reversed list
Walkthrough
The recursive approach works by:
- First, we need to reach the end of the linked list through recursion
- As we backtrack, we reverse the links between nodes
- Finally, we set the head's next to null to complete the reversal
Here's the implementation:
public ListNode reverseList(ListNode head) { // Base cases: if head is null or we've reached the last node if (head == null || head.next == null) { return head; } // Recursively reach the last node ListNode reversedList = reverseList(head.next); // Reverse the link between current node and next node head.next.next = head; head.next = null; return reversedList;}Let's see how it works with example [1,2,3]:
- First recursive call: head = 1, calls reverseList(2)
- Second recursive call: head = 2, calls reverseList(3)
- Third recursive call: head = 3, returns 3 as it's the last node
- Backtrack to 2: makes 3->2, 2->null
- Backtrack to 1: makes 2->1, 1->null
- Final result: 3->2->1->null
Complexity
Time
O(n) where n is the number of nodes in the linked list - we need to traverse each node once
Space
O(n) due to the recursive call stack - we make n recursive calls for n nodes
Trade-offs
Pros
Clean and elegant solution
Easy to understand the logic
Good for learning recursive thinking
Cons
Uses extra space due to recursive call stack
Can cause stack overflow for very large lists
Not as space efficient as iterative approach
Solutions
Solution
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int val=0, ListNode next=null) { * this.val = val; * this.next = next; * } * } */ public class Solution { public ListNode ReverseList ( ListNode head ) { ListNode pre = null ; for ( ListNode p = head ; p != null ;) { ListNode t = p . next ; p . next = pre ; pre = p ; p = t ; } return pre ; } }Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.