0
0
DSA Pythonprogramming

Reverse a Singly Linked List Recursive in DSA Python

Choose your learning style9 modes available
Mental Model
We flip the direction of each link by going to the end first, then reversing as we return back.
Analogy: Imagine unstacking a pile of plates one by one until the last plate, then stacking them back in reverse order.
head -> 1 -> 2 -> 3 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null
Goal: Reverse the list so it becomes 3 -> 2 -> 1 -> null using recursion
Step 1: call reverse on node 1, which calls reverse on node 2
head -> 1 -> 2 -> 3 -> null ↑ recursion goes deeper
Why: We go to the end before reversing links
Step 2: call reverse on node 2, which calls reverse on node 3
head -> 1 -> 2 -> 3 -> null ↑ recursion goes deeper
Why: Keep going deeper to reach last node
Step 3: call reverse on node 3, base case reached, return node 3
head -> 1 -> 2 -> 3 -> null ↑ at last node
Why: Last node becomes new head of reversed list
Step 4: reverse link: node 3 points back to node 2, node 2 next set to null
head -> 1 -> 2 ← 3 null ↑ reversing links
Why: Flip the link direction to reverse
Step 5: reverse link: node 2 points back to node 1, node 1 next set to null
head ← 1 ← 2 ← 3 null ↑ reversing links
Why: Continue flipping links as recursion unwinds
Step 6: return new head node 3 up to original call
3 -> 2 -> 1 -> null
Why: Final reversed list head returned
Result:
3 -> 2 -> 1 -> null
Annotated Code
DSA Python
class Node:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def reverse_recursive(head):
    if head is None or head.next is None:
        return head  # base case: empty or single node
    new_head = reverse_recursive(head.next)  # reverse rest
    head.next.next = head  # flip link of next node back to current
    head.next = None  # set current next to None to avoid cycle
    return new_head

def print_list(head):
    curr = head
    result = []
    while curr:
        result.append(str(curr.val))
        curr = curr.next
    print(" -> ".join(result) + " -> null")

# driver code
head = Node(1, Node(2, Node(3)))
print_list(head)
reversed_head = reverse_recursive(head)
print_list(reversed_head)
if head is None or head.next is None:
check base case: empty or single node list
new_head = reverse_recursive(head.next)
recursively reverse the rest of the list
head.next.next = head
flip the next node's next pointer back to current node
head.next = None
set current node's next to None to avoid cycle
return new_head
return new head of reversed list up the call stack
OutputSuccess
1 -> 2 -> 3 -> null 3 -> 2 -> 1 -> null
Complexity Analysis
Time: O(n) because we visit each node once during recursion
Space: O(n) due to recursion call stack for n nodes
vs Alternative: Iterative reversal uses O(1) space but recursion is simpler to write and understand
Edge Cases
empty list (head is None)
returns None immediately without error
DSA Python
if head is None or head.next is None:
single node list
returns the single node as is
DSA Python
if head is None or head.next is None:
When to Use This Pattern
When you see a problem asking to reverse a linked list recursively, think about reaching the end first then flipping links on the way back.
Common Mistakes
Mistake: Not setting head.next to None after flipping the link, causing cycles
Fix: Always set head.next = None after reversing the link to break old forward connection
Mistake: Returning head instead of new_head after recursion
Fix: Return new_head which is the new head of reversed list from recursive calls
Summary
Reverses a singly linked list by recursively flipping links from the end back to the start.
Use when you want a clean recursive solution to reverse a linked list.
The key insight is to reverse the rest first, then flip the current node's link.