0
0
DSA Pythonprogramming~10 mins

Reverse a Singly Linked List Recursive in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Reverse a Singly Linked List Recursive
Start with head node
Check if node is None or last node
Yes
Return node as new head
No
Recursive call on next node
Reverse link: next.next = current
Set current.next = None
Return new head from recursion
The recursion goes to the last node, then reverses links on the way back, setting each next node to point back to current.
Execution Sample
DSA Python
def reverse_recursive(node):
    if node is None or node.next is None:
        return node
    new_head = reverse_recursive(node.next)
    node.next.next = node
    node.next = None
    return new_head
This function reverses a singly linked list by recursively reversing the rest and then fixing the current node's link.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Call reverse_recursive on node 11 -> 2 -> 3 -> NoneNone1 -> 2 -> 3 -> None
2Call reverse_recursive on node 21 -> 2 -> 3 -> NoneNone1 -> 2 -> 3 -> None
3Call reverse_recursive on node 31 -> 2 -> 3 -> NoneNone1 -> 2 -> 3 -> None
4Base case reached at node 3, return node 31 -> 2 -> 3 -> NoneNone1 -> 2 -> 3 -> None
5Reverse link: node 2.next.next = node 21 -> 2 -> 3 -> None3.next = 21 -> 2 -> 3 -> 2 (cycle temporarily)
6Set node 2.next = None1 -> 2 -> None, 3 -> 22.next = None1 -> 2 -> None, 3 -> 2
7Return new head node 33 -> 2 -> None, 1 -> 2 (old link)None3 -> 2 -> None, 1 -> 2 (old link)
8Reverse link: node 1.next.next = node 13 -> 2 -> None, 1 -> 22.next = 13 -> 2 -> 1
9Set node 1.next = None3 -> 2 -> 1 -> None1.next = None3 -> 2 -> 1 -> None
10Return new head node 33 -> 2 -> 1 -> NoneNone3 -> 2 -> 1 -> None
💡 Recursion ends when node is None or node.next is None (last node reached)
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 6After Step 9Final
node1233213
new_headNoneNone33333
node.next23NoneNoneNoneNoneNone
Key Moments - 3 Insights
Why do we set node.next = None after reversing the link?
Because after reversing, the current node's next still points forward, causing a cycle. Setting node.next = None breaks the old forward link, as shown in steps 6 and 9.
Why does the recursion stop at node.next == None?
Because the last node has no next node, so it becomes the new head. This base case is shown in step 4 where recursion returns the last node.
How does the list look during the reversal steps?
During reversal (steps 5 and 8), links temporarily point backward creating a reversed partial list, but old forward links remain until we set node.next = None to finalize.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the 'node' value at Step 3?
A2
B3
C1
DNone
💡 Hint
Check the 'node' variable in variable_tracker after Step 3
At which step does the recursion reach the base case?
AStep 4
BStep 6
CStep 2
DStep 9
💡 Hint
Look for the step where the function returns the last node without further recursion
If we forget to set node.next = None, what happens to the list?
AThe list becomes empty
BThe list remains unchanged
CThe list has cycles causing infinite loops
DThe list is reversed correctly
💡 Hint
Refer to key_moments explanation about breaking old links after reversal
Concept Snapshot
Reverse a singly linked list recursively:
- Base case: node is None or node.next is None
- Recursively reverse rest of list
- Set node.next.next = node to reverse link
- Set node.next = None to break old link
- Return new head from recursion
Full Transcript
This visualization shows how to reverse a singly linked list using recursion. The function calls itself until it reaches the last node, which becomes the new head. Then, on returning from recursion, it reverses the links by pointing the next node back to the current node and breaking the old forward link by setting current node's next to None. The execution table tracks each step, showing how the list nodes and pointers change. Key moments clarify why breaking the old link is necessary and when recursion stops. The quiz tests understanding of node values at steps, base case detection, and consequences of missing link updates.