Reverse a Singly Linked List Recursive in DSA C# - 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 class Node {
public int Value;
public Node Next;
public Node(int val) { Value = val; Next = null; }
}
public Node ReverseRecursive(Node head) {
if (head == null || head.Next == null) return head;
Node newHead = ReverseRecursive(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 rewires the links backward.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call on the next node in 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 steps grow directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 recursive calls and rewiring steps |
| 100 | About 100 recursive calls and rewiring steps |
| 1000 | About 1000 recursive calls and rewiring steps |
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 faster because it uses recursion instead of loops."
[OK] Correct: Both recursion and loops visit each node once, so they take similar time. Recursion adds function call overhead but does not reduce the number of steps.
Understanding recursive time complexity helps you explain your code clearly and shows you know how your solution scales with input size.
"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's pointer after recursion
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 DQuick Check:
Recursive reversal = reverse rest then fix current [OK]
- Trying to swap node values instead of pointers
- Not fixing the current node's next pointer after recursion
- Using iteration instead of recursion
Solution
Step 1: Identify when recursion should stop
The base case is when the list is empty or has only one node, so no reversal needed.Step 2: Check the correct base case syntax
Return the head itself if it is null or the last node (head.next == null).Final Answer:
if (head == null || head.next == null) return head; -> Option CQuick Check:
Base case = empty or single node returns head [OK]
- Returning null when list has one node
- Accessing head.next without checking if head is null
- Creating new nodes unnecessarily
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 calls
Reverse(1) calls Reverse(2), which calls Reverse(3). Reverse(3) returns node 3 as base case.Step 2: Fix pointers on return
head.next.next = head reverses link: 3.next = 2, then 2.next = 1, and 1.next = null.Final Answer:
3 -> 2 -> 1 -> null -> Option AQuick Check:
Recursive reversal output = reversed list [OK]
- Not setting head.next to null causing cycle
- Confusing node values with pointers
- Returning wrong node as new head
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 (head.next == null).Step 2: Consequence of missing base case
Without single node base case, head.next may be null causing head.next.next to throw error.Final Answer:
Missing base case for single node; add 'head.next == null' check -> Option BQuick Check:
Base case must handle empty and single node [OK]
- Ignoring single node base case
- Not setting head.next = null after reversal
- Returning wrong node as new head
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;
}
// After reversal, print list from newHeadSolution
Step 1: Trace recursive reversal on given list
Recursion reverses from last node 40 back to 10, fixing pointers step-by-step.Step 2: Confirm final reversed list order
After reversal, list order is 40 -> 30 -> 20 -> 10 -> null.Final Answer:
40 -> 30 -> 20 -> 10 -> null -> Option AQuick Check:
Recursive reversal reverses entire list order [OK]
- Printing original list instead of reversed
- Forgetting to set head.next = null causing cycle
- Misreading pointer changes as value swaps
Ready to prove it?
Pass the test and this pattern moves unknown
