Challenge - 5 Problems
Recursive Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* reverseRecursive(Node* head) {
if (!head || !head->next) return head;
Node* newHead = reverseRecursive(head->next);
head->next->next = head;
head->next = nullptr;
return newHead;
}
void printList(Node* head) {
while (head) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head = reverseRecursive(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Think about how the recursion unwinds and reverses the links one by one.
✗ Incorrect
The recursive function reverses the list by moving to the end and then changing the next pointers backward. The final list is reversed: 3 -> 2 -> 1 -> null.
❓ Predict Output
intermediateOutput 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++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* reverseRecursive(Node* head) {
if (!head || !head->next) return head;
Node* newHead = reverseRecursive(head->next);
head->next->next = head;
head->next = nullptr;
return newHead;
}
void printList(Node* head) {
while (head) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = new Node(5);
head = reverseRecursive(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
A single node reversed is itself.
✗ Incorrect
The base case returns the head immediately if there is no next node, so the list remains unchanged.
🔧 Debug
advancedIdentify the Error in Recursive Reverse Implementation
What error will this recursive reverse function cause when called on a list with multiple nodes?
DSA C++
Node* reverseRecursive(Node* head) {
if (!head) return nullptr;
Node* newHead = reverseRecursive(head->next);
head->next->next = head;
head->next = nullptr;
return newHead;
}Attempts:
2 left
💡 Hint
Check the base case carefully and what happens when head->next is nullptr.
✗ Incorrect
The base case only checks if head is nullptr, but does not check if head->next is nullptr. When head->next is nullptr, head->next->next causes dereferencing nullptr leading to runtime error.
🧠 Conceptual
advancedUnderstanding Recursive Call Stack in List Reversal
During the recursive reversal of a singly linked list, what happens first when the recursion unwinds?
Attempts:
2 left
💡 Hint
Think about how recursion reaches the end before changing links.
✗ Incorrect
Recursion goes to the last node first, which becomes the new head. Then as it returns back, it reverses the links one by one.
❓ Predict Output
expertOutput of Recursive Reverse on a 5-Node List with Duplicate Values
What is the printed state of the linked list after calling the recursive reverse function on this list?
Initial list: 4 -> 7 -> 7 -> 2 -> 4 -> null
Initial list: 4 -> 7 -> 7 -> 2 -> 4 -> null
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* reverseRecursive(Node* head) {
if (!head || !head->next) return head;
Node* newHead = reverseRecursive(head->next);
head->next->next = head;
head->next = nullptr;
return newHead;
}
void printList(Node* head) {
while (head) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = new Node(4);
head->next = new Node(7);
head->next->next = new Node(7);
head->next->next->next = new Node(2);
head->next->next->next->next = new Node(4);
head = reverseRecursive(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
The reverse function reverses the entire list order, duplicates remain in reversed positions.
✗ Incorrect
The list is reversed completely, so the last node (4) becomes the head, followed by 2, then 7, 7, and finally the original head 4 at the end.
