0
0
DSA Pythonprogramming~20 mins

Reverse a Singly Linked List Recursive in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Recursive Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Recursive Linked List Reversal
What is the printed state of the linked list after reversing it recursively?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

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

def reverse_recursive(head):
    if head is None or head.next is None:
        return head
    new_head = reverse_recursive(head.next)
    head.next.next = head
    head.next = None
    return new_head

# Create linked list 1 -> 2 -> 3 -> 4 -> null
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)

head = reverse_recursive(head)
print_list(head)
Anull
B1 -> 2 -> 3 -> 4 -> null
C4 -> 3 -> 2 -> 1 -> null
D1 -> null
Attempts:
2 left
💡 Hint
Think about how the recursion unwinds and reverses the links one by one.
Predict Output
intermediate
2:00remaining
Output when reversing a single-node list recursively
What is the printed state of the linked list after reversing a single-node list recursively?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

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

def reverse_recursive(head):
    if head is None or head.next is None:
        return head
    new_head = reverse_recursive(head.next)
    head.next.next = head
    head.next = None
    return new_head

# Create linked list 5 -> null
head = Node(5)

head = reverse_recursive(head)
print_list(head)
A5 -> null
Bnull
C5 -> 5 -> null
DError: NoneType has no attribute 'next'
Attempts:
2 left
💡 Hint
A single-node list reversed is the same list.
Predict Output
advanced
2:00remaining
Output of recursive reversal on empty list
What is the printed output when reversing an empty linked list recursively?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def print_list(head):
    if head is None:
        print('null')
        return
    current = head
    result = []
    while current:
        result.append(str(current.val))
        current = current.next
    print(' -> '.join(result) + ' -> null')

def reverse_recursive(head):
    if head is None or head.next is None:
        return head
    new_head = reverse_recursive(head.next)
    head.next.next = head
    head.next = None
    return new_head

head = None
head = reverse_recursive(head)
print_list(head)
AError: 'NoneType' object has no attribute 'next'
Bnull
CEmpty list
DNone
Attempts:
2 left
💡 Hint
Check the base case for empty list in the recursive function.
🔧 Debug
advanced
2:00remaining
Identify the error in this recursive reversal code
What error will this code produce when reversing a linked list recursively?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def reverse_recursive(head):
    if head is None or head.next is None:
        return head
    new_head = reverse_recursive(head.next)
    head.next = head
    head.next = None
    return new_head

# Create linked list 1 -> 2 -> null
head = Node(1)
head.next = Node(2)

head = reverse_recursive(head)
ARecursionError: maximum recursion depth exceeded
BAttributeError: 'NoneType' object has no attribute 'next'
CNo error, works correctly
DTypeError: unsupported operand type(s)
Attempts:
2 left
💡 Hint
Check the line where the next pointer is assigned to itself.
🧠 Conceptual
expert
2:00remaining
Why does the recursive reversal function set head.next.next = head?
In the recursive reversal of a singly linked list, why do we set 'head.next.next = head' before setting 'head.next = None'?
ATo delete the current node from the list
BTo break the link between current node and next node to avoid cycles
CTo move the head pointer forward in the list
DTo reverse the link by making the next node point back to the current node
Attempts:
2 left
💡 Hint
Think about how links are reversed step by step in recursion.