Reverse a Singly Linked List Recursive in DSA Java - Time & Space Complexity
We want to understand how the time needed to reverse a linked list using recursion changes as the list grows.
How does the number of steps grow when the list gets longer?
Analyze the time complexity of the following code snippet.
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
This code reverses a singly linked list by calling itself on the next node until it reaches the end, then reverses the links on the way back.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call on the next node of the list.
- How many times: Once for each node in the list, so n times for a list of size n.
Each node causes one recursive call, so the total steps grow directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 recursive calls and pointer changes |
| 100 | About 100 recursive calls and pointer changes |
| 1000 | About 1000 recursive calls and pointer changes |
Pattern observation: The number of operations grows linearly as the list size increases.
Time Complexity: O(n)
This means the time to reverse the list grows in direct proportion to the number of nodes.
[X] Wrong: "Recursive reversal is slower because it does extra work on each call like loops inside loops."
[OK] Correct: Each recursive call only processes one node and does a constant amount of work, so it does not multiply work like nested loops.
Understanding recursive reversal shows you can work with linked lists and recursion clearly, a skill often checked in interviews.
"What if we changed the recursive reversal to an iterative approach? How would the time complexity change?"
Practice
Solution
Step 1: Understand recursion on linked list
Recursion works by solving a smaller part first, here reversing from the next node onward.Step 2: Fix current node after recursive call
After reversing the rest, the current node's next node points back to it, reversing the link.Final Answer:
Reverse the rest of the list first, then fix the current node's next pointer -> Option AQuick Check:
Recursive reversal = Reverse rest then fix current [OK]
- Trying to swap node values instead of pointers
- Reversing pointers before recursive call
- Using iteration instead of recursion
Solution
Step 1: Identify base case meaning
The base case stops recursion when list is empty or has one node, so head is null or head.next is null.Step 2: Check each option
if (head == null || head.next == null) return head; correctly returns head when list is empty or single node. Others cause errors or wrong returns.Final Answer:
if (head == null || head.next == null) return head; -> Option DQuick Check:
Base case = empty or single node returns head [OK]
- Returning null when list has one node
- Accessing next of null causing errors
- Missing base case leading to infinite recursion
Node reverse(Node head) {
if (head == null || head.next == null) return head;
Node newHead = reverse(head.next);
head.next.next = head;
head.next = null;
return newHead;
}Solution
Step 1: Trace recursion on list 1->2->3
Recursion goes to last node 3, then reverses links backward: 3 points to 2, 2 points to 1.Step 2: Final reversed list
After fixing pointers, the list becomes 3 -> 2 -> 1 -> null.Final Answer:
3 -> 2 -> 1 -> null -> Option CQuick Check:
Reversed list = 3 -> 2 -> 1 [OK]
- Not setting head.next to null causing cycle
- Returning old head instead of new head
- Confusing node values with pointers
Node reverse(Node head) {
if (head == null) return null;
Node newHead = reverse(head.next);
head.next.next = head;
return newHead;
}Solution
Step 1: Check base case correctness
Base case only checks head == null, missing single node case causes null pointer error at head.next.next.Step 2: Fix base case
Adding 'head.next == null' to base case prevents error and correctly stops recursion.Final Answer:
Missing base case for single node; add 'head.next == null' check -> Option BQuick Check:
Base case must handle single node [OK]
- Ignoring single node base case causing runtime error
- Incorrect pointer assignment causing infinite loop
- Returning wrong node after recursion
class Result {
Node head;
int count;
}
Result reverseCount(Node head) {
if (head == null || head.next == null) {
Result res = new Result();
res.head = head;
res.count = (head == null) ? 0 : 1;
return res;
}
Result res = reverseCount(head.next);
head.next.next = head;
head.next = null;
res.count += 1;
return res;
}Solution
Step 1: Analyze recursive counting logic
Base case sets count to 1 or 0. Each recursive return increments count by 1 after reversing pointer.Step 2: Confirm correctness of pointer reversal and counting
Pointer reversal is correct and count increments after recursion, so total nodes counted correctly.Final Answer:
This code correctly reverses and counts nodes -> Option AQuick Check:
Counting nodes during recursion works as coded [OK]
- Incrementing count before recursion causing wrong totals
- Removing head.next = null causing cycles
- Using static variable causing shared state bugs
Ready to prove it?
Pass the test and this pattern moves unknown
