Challenge - 5 Problems
Recursive Reverse Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Recursive Reverse on a 3-Node List
What is the printed state of the linked list after calling the recursive reverse function on this list?
Initial list: 1 -> 2 -> 3 -> null
Initial list: 1 -> 2 -> 3 -> null
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* reverseRecursive(Node* head) {
if (head == NULL || head->next == NULL) {
return head;
}
Node* rest = reverseRecursive(head->next);
head->next->next = head;
head->next = NULL;
return rest;
}
// After calling reverseRecursive on list 1->2->3->NULL, print the listAttempts:
2 left
💡 Hint
Think about how the recursion reaches the last node and then reverses the links step by step.
✗ Incorrect
The recursive function goes to the last node (3), then reverses the links so that 3 points to 2, and 2 points to 1, and finally 1 points to NULL. So the list is reversed as 3 -> 2 -> 1 -> null.
❓ Predict Output
intermediate2:00remaining
Output of Recursive Reverse on Single Node List
What is the printed state of the linked list after calling the recursive reverse function on a single-node list?
Initial list: 5 -> null
Initial list: 5 -> null
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* reverseRecursive(Node* head) {
if (head == NULL || head->next == NULL) {
return head;
}
Node* rest = reverseRecursive(head->next);
head->next->next = head;
head->next = NULL;
return rest;
}
// After calling reverseRecursive on list 5->NULL, print the listAttempts:
2 left
💡 Hint
A single node reversed is still the same node.
✗ Incorrect
The base case returns the head immediately if the list has only one node or is empty, so the list remains unchanged.
🔧 Debug
advanced2:00remaining
Identify the Bug in Recursive Reverse Function
What error will this recursive reverse function cause when called on a list with multiple nodes?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* reverseRecursive(Node* head) {
if (head == NULL || head->next == NULL) {
return head;
}
Node* rest = reverseRecursive(head->next);
head->next = NULL;
head->next->next = head;
return rest;
}Attempts:
2 left
💡 Hint
Look carefully at the order of pointer assignments after recursion.
✗ Incorrect
The line 'head->next = NULL;' sets head->next to NULL before 'head->next->next = head;' is executed, so head->next is NULL and accessing head->next->next causes a segmentation fault.
❓ Predict Output
advanced2:00remaining
Output of Recursive Reverse on 4-Node List
What is the printed state of the linked list after calling the recursive reverse function on this list?
Initial list: 10 -> 20 -> 30 -> 40 -> null
Initial list: 10 -> 20 -> 30 -> 40 -> null
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* reverseRecursive(Node* head) {
if (head == NULL || head->next == NULL) {
return head;
}
Node* rest = reverseRecursive(head->next);
head->next->next = head;
head->next = NULL;
return rest;
}
// After calling reverseRecursive on list 10->20->30->40->NULL, print the listAttempts:
2 left
💡 Hint
The recursive function reverses the list by changing the next pointers step by step from the end.
✗ Incorrect
The recursion reaches the last node (40), then reverses the links so that 40 points to 30, 30 points to 20, 20 points to 10, and 10 points to NULL.
🧠 Conceptual
expert2:00remaining
Why Does Recursive Reverse Use head->next->next = head?
In the recursive reverse function for a singly linked list, why do we assign 'head->next->next = head;' instead of 'head->next = head;'?
Attempts:
2 left
💡 Hint
Think about how pointers must be reversed to flip the list direction.
✗ Incorrect
The assignment 'head->next->next = head;' makes the next node point back to the current node, reversing the link. Assigning 'head->next = head;' would make the current node point to itself, causing a cycle.
