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 calling the recursive reverse function on the list 1 -> 2 -> 3 -> null?
DSA C#
public class Node { public int val; public Node next; public Node(int val) { this.val = val; this.next = null; } } public class LinkedList { public Node head; public Node ReverseRecursive(Node node) { if (node == null || node.next == null) return node; Node newHead = ReverseRecursive(node.next); node.next.next = node; node.next = null; return newHead; } public void PrintList(Node node) { while (node != null) { System.Console.Write(node.val + " -> "); node = node.next; } System.Console.WriteLine("null"); } } // Setup LinkedList list = new LinkedList(); list.head = new Node(1); list.head.next = new Node(2); list.head.next.next = new Node(3); // Reverse list.head = list.ReverseRecursive(list.head); list.PrintList(list.head);
Attempts:
2 left
💡 Hint
Think about how the recursion reverses the links starting from the end of the list.
✗ Incorrect
The recursive function reverses the list by reaching the last node and then rewiring the next pointers backward. The final list is reversed: 3 -> 2 -> 1 -> null.
❓ Predict Output
intermediateOutput After 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 5 -> null?
DSA C#
public class Node { public int val; public Node next; public Node(int val) { this.val = val; this.next = null; } } public class LinkedList { public Node head; public Node ReverseRecursive(Node node) { if (node == null || node.next == null) return node; Node newHead = ReverseRecursive(node.next); node.next.next = node; node.next = null; return newHead; } public void PrintList(Node node) { while (node != null) { System.Console.Write(node.val + " -> "); node = node.next; } System.Console.WriteLine("null"); } } // Setup LinkedList list = new LinkedList(); list.head = new Node(5); // Reverse list.head = list.ReverseRecursive(list.head); list.PrintList(list.head);
Attempts:
2 left
💡 Hint
A single node reversed is itself.
✗ Incorrect
The base case returns the node itself when next is null, 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#
public Node ReverseRecursive(Node node) {
if (node == null) return null;
Node newHead = ReverseRecursive(node.next);
node.next.next = node;
node.next = null;
return newHead;
}Attempts:
2 left
💡 Hint
Check the base case carefully.
✗ Incorrect
The base case is missing the condition to return when node.next is null, so node.next.next causes NullReferenceException.
🧠 Conceptual
advancedWhy Does Recursive Reverse Use node.next.next = node?
In the recursive reverse function, why do we assign 'node.next.next = node'?
Attempts:
2 left
💡 Hint
Think about how links are reversed step by step.
✗ Incorrect
Assigning 'node.next.next = node' reverses the direction of the link between current node and next node.
❓ Predict Output
expertOutput After Recursive Reverse on 4-Node List with Values 10, 20, 30, 40
What is the printed state of the linked list after calling the recursive reverse function on the list 10 -> 20 -> 30 -> 40 -> null?
DSA C#
public class Node { public int val; public Node next; public Node(int val) { this.val = val; this.next = null; } } public class LinkedList { public Node head; public Node ReverseRecursive(Node node) { if (node == null || node.next == null) return node; Node newHead = ReverseRecursive(node.next); node.next.next = node; node.next = null; return newHead; } public void PrintList(Node node) { while (node != null) { System.Console.Write(node.val + " -> "); node = node.next; } System.Console.WriteLine("null"); } } // Setup LinkedList list = new LinkedList(); list.head = new Node(10); list.head.next = new Node(20); list.head.next.next = new Node(30); list.head.next.next.next = new Node(40); // Reverse list.head = list.ReverseRecursive(list.head); list.PrintList(list.head);
Attempts:
2 left
💡 Hint
The recursive reverse flips the list completely.
✗ Incorrect
The recursive function reverses the list fully, so the last node becomes the head and links go backward.
