Challenge - 5 Problems
Recursive Reverse 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 running the recursive reverse function on this list?
Initial list: 1 -> 2 -> 3 -> null
Initial list: 1 -> 2 -> 3 -> null
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function reverseRecursive(head) { if (!head || !head.next) return head; const newHead = reverseRecursive(head.next); head.next.next = head; head.next = null; return newHead; } // Setup list const head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); const reversedHead = reverseRecursive(head); // Function to print list function printList(node) { let result = ''; while (node) { result += node.value + ' -> '; node = node.next; } result += 'null'; console.log(result); } printList(reversedHead);
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 going to the end and then rewiring the next pointers backward. The final printed list is reversed: 3 -> 2 -> 1 -> null.
❓ Predict Output
intermediateOutput When Reversing a Single Node List
What will be the printed output after reversing a linked list with only one node (value 5) using the recursive reverse function?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function reverseRecursive(head) { if (!head || !head.next) return head; const newHead = reverseRecursive(head.next); head.next.next = head; head.next = null; return newHead; } const head = new Node(5); const reversedHead = reverseRecursive(head); function printList(node) { let result = ''; while (node) { result += node.value + ' -> '; node = node.next; } result += 'null'; console.log(result); } printList(reversedHead);
Attempts:
2 left
💡 Hint
A single node reversed is still the same node.
✗ Incorrect
Since the list has only one node, the base case returns the node itself without changes. The output is '5 -> null'.
❓ Predict Output
advancedOutput of Recursive Reverse on a 4-Node List with Values 10, 20, 30, 40
What is the printed state of the linked list after running the recursive reverse function on this list?
Initial list: 10 -> 20 -> 30 -> 40 -> null
Initial list: 10 -> 20 -> 30 -> 40 -> null
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function reverseRecursive(head) { if (!head || !head.next) return head; const newHead = reverseRecursive(head.next); head.next.next = head; head.next = null; return newHead; } const head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); head.next.next.next = new Node(40); const reversedHead = reverseRecursive(head); function printList(node) { let result = ''; while (node) { result += node.value + ' -> '; node = node.next; } result += 'null'; console.log(result); } printList(reversedHead);
Attempts:
2 left
💡 Hint
The recursion reverses the list by rewiring the next pointers from the end back to the start.
✗ Incorrect
The recursive function reverses the list completely. The final printed list is reversed: 40 -> 30 -> 20 -> 10 -> null.
🧠 Conceptual
advancedUnderstanding the Base Case in Recursive Reverse
Why is the base case 'if (!head || !head.next) return head;' necessary in the recursive reverse function for a singly linked list?
Attempts:
2 left
💡 Hint
Think about what happens when the function reaches the end of the list.
✗ Incorrect
The base case stops recursion when the list is empty or has only one node, which is the new head of the reversed list. Without it, recursion would continue infinitely or cause errors.
❓ Predict Output
expertOutput After Recursive Reverse on a Circular Linked List
Given a circular singly linked list where the last node points back to the head, what happens when you run the standard recursive reverse function below?
Code snippet:
Code snippet:
function reverseRecursive(head) {
if (!head || !head.next) return head;
const newHead = reverseRecursive(head.next);
head.next.next = head;
head.next = null;
return newHead;
}DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } // Create circular list: 1 -> 2 -> 3 -> 1 ... const head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = head; // circular link function reverseRecursive(head) { if (!head || !head.next) return head; const newHead = reverseRecursive(head.next); head.next.next = head; head.next = null; return newHead; } // Attempt to reverse circular list const reversedHead = reverseRecursive(head); function printList(node, limit = 10) { let result = ''; let count = 0; while (node && count < limit) { result += node.value + ' -> '; node = node.next; count++; } result += '...'; console.log(result); } printList(reversedHead);
Attempts:
2 left
💡 Hint
Consider what happens when the recursion never reaches a null next pointer in a circular list.
✗ Incorrect
Because the list is circular, the recursion never reaches a node with null next, causing infinite recursion and eventually a stack overflow error.
