Bird
Raised Fist0
DSA Pythonprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the recursive function to reverse a singly linked list primarily do at each step?
easy
A. It iterates through the list using a loop to reverse pointers.
B. It swaps the values of the first and last nodes only.
C. It reverses the rest of the list and then adjusts the current node's next pointer.
D. It creates a new list and copies nodes in reverse order.

Solution

  1. Step 1: Understand recursion on linked list

    The function calls itself on the next node until it reaches the end (base case).
  2. Step 2: Reverse pointers on return

    After reaching the end, it reverses the next pointer of the current node to point backward.
  3. Final Answer:

    It reverses the rest of the list and then adjusts the current node's next pointer. -> Option C
  4. Quick Check:

    Recursive reversal = reverse rest + fix pointers [OK]
Hint: Think: reverse rest first, then fix current pointer [OK]
Common Mistakes:
  • Trying to swap node values instead of pointers
  • Using loops instead of recursion
  • Not handling the base case properly
2. Which of the following is the correct base case for a recursive function reversing a singly linked list?
easy
A. if head.next is None: return None
B. if head.next is None: return head.next
C. if head is None: return head.next
D. if head is None or head.next is None: return head

Solution

  1. Step 1: Identify base case meaning

    The base case stops recursion when the list is empty or has one node.
  2. Step 2: Check correct condition

    When head is None (empty) or head.next is None (single node), return head as reversed list.
  3. Final Answer:

    if head is None or head.next is None: return head -> Option D
  4. Quick Check:

    Base case = empty or single node [OK]
Hint: Base case: empty or single node returns itself [OK]
Common Mistakes:
  • Returning None incorrectly for single node
  • Missing the empty list check
  • Returning head.next instead of head
3. What will be the output linked list after reversing recursively the list: 1 -> 2 -> 3 -> None?
medium
A. 1 -> 2 -> 3 -> None
B. 3 -> 2 -> 1 -> None
C. 2 -> 3 -> 1 -> None
D. None -> 3 -> 2 -> 1

Solution

  1. Step 1: Trace recursive calls

    Function calls itself on 2 -> 3 -> None, then on 3 -> None (base case).
  2. Step 2: Reverse pointers on return

    On return, 3 points to 2, 2 points to 1, and 1 points to None.
  3. Final Answer:

    3 -> 2 -> 1 -> None -> Option B
  4. Quick Check:

    Reversed list = 3 -> 2 -> 1 [OK]
Hint: Last node becomes new head after recursion [OK]
Common Mistakes:
  • Not updating the last node's next pointer
  • Returning original head instead of new head
  • Confusing node values with pointers
4. Given this recursive reverse function, what is the error?
def reverse(head):
    if head is None:
        return None
    new_head = reverse(head.next)
    head.next.next = head
    head.next = None
    return new_head
medium
A. Base case misses single node check causing AttributeError
B. head.next.next = head causes infinite loop
C. head.next = None should be before recursive call
D. Function returns None always

Solution

  1. Step 1: Analyze base case

    Base case only checks if head is None, but not if head.next is None (single node).
  2. Step 2: Identify error cause

    When head.next is None, head.next.next causes AttributeError because head.next is None.
  3. Final Answer:

    Base case misses single node check causing AttributeError -> Option A
  4. Quick Check:

    Missing single node base case = error [OK]
Hint: Always check head.next is None in base case [OK]
Common Mistakes:
  • Ignoring single node base case
  • Changing pointers before recursion
  • Returning None instead of new head
5. How would you modify the recursive reverse function to also return the tail node of the reversed list efficiently?
hard
A. Return a tuple (new_head, tail) and update tail during recursion
B. Use a global variable to track tail node
C. Traverse the reversed list again to find tail after recursion
D. Return only new_head and ignore tail

Solution

  1. Step 1: Understand tail tracking need

    To get tail efficiently, return it along with new head during recursion.
  2. Step 2: Modify function to return tuple

    Return (new_head, tail) from recursive calls and update tail as current node.
  3. Step 3: Avoid extra traversal

    This avoids traversing reversed list again, improving efficiency.
  4. Final Answer:

    Return a tuple (new_head, tail) and update tail during recursion -> Option A
  5. Quick Check:

    Return (head, tail) tuple for efficiency [OK]
Hint: Return both head and tail as tuple in recursion [OK]
Common Mistakes:
  • Using global variables causing side effects
  • Traversing list again after reversal
  • Ignoring tail node requirement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard