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 recursively reversing the list 1 -> 2 -> 3 -> null?
DSA Java
class Node { int val; Node next; Node(int val) { this.val = val; this.next = null; } } public class Main { public static Node reverseRecursive(Node head) { if (head == null || head.next == null) return head; Node newHead = reverseRecursive(head.next); head.next.next = head; head.next = null; return newHead; } public static void printList(Node head) { while (head != null) { System.out.print(head.val + " -> "); head = head.next; } System.out.print("null"); } public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head = reverseRecursive(head); printList(head); } }
Attempts:
2 left
💡 Hint
Think about how the recursion reverses the links starting from the end.
✗ Incorrect
The recursive function reverses the list by going to the end and then rewiring the next pointers backwards. The original head becomes the last node with next set to null.
❓ Predict Output
intermediateOutput of Recursive Reverse on Single Node List
What is the printed state of the linked list after recursively reversing the list 5 -> null?
DSA Java
class Node { int val; Node next; Node(int val) { this.val = val; this.next = null; } } public class Main { public static Node reverseRecursive(Node head) { if (head == null || head.next == null) return head; Node newHead = reverseRecursive(head.next); head.next.next = head; head.next = null; return newHead; } public static void printList(Node head) { while (head != null) { System.out.print(head.val + " -> "); head = head.next; } System.out.print("null"); } public static void main(String[] args) { Node head = new Node(5); head = reverseRecursive(head); printList(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 code produce when trying to reverse a linked list recursively?
DSA Java
class Node { int val; Node next; Node(int val) { this.val = val; this.next = null; } } public class Main { public static Node reverseRecursive(Node head) { if (head == null || head.next == null) return head; head.next = head; Node newHead = reverseRecursive(head.next); head.next = null; return newHead; } }
Attempts:
2 left
💡 Hint
Check how the pointers are reassigned inside the recursion.
✗ Incorrect
The line 'head.next = head;' creates a cycle causing infinite recursion and eventually a StackOverflowError.
🧠 Conceptual
advancedWhy is the Base Case Important in Recursive Reverse?
In the recursive reversal of a singly linked list, why do we check if head is null or head.next is null as the base case?
Attempts:
2 left
💡 Hint
Think about when recursion should stop.
✗ Incorrect
The base case stops recursion when the list is empty or has one node, which doesn't need reversal.
❓ 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 recursively reversing the list 10 -> 20 -> 30 -> 40 -> null?
DSA Java
class Node { int val; Node next; Node(int val) { this.val = val; this.next = null; } } public class Main { public static Node reverseRecursive(Node head) { if (head == null || head.next == null) return head; Node newHead = reverseRecursive(head.next); head.next.next = head; head.next = null; return newHead; } public static void printList(Node head) { while (head != null) { System.out.print(head.val + " -> "); head = head.next; } System.out.print("null"); } public static void main(String[] args) { Node head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); head.next.next.next = new Node(40); head = reverseRecursive(head); printList(head); } }
Attempts:
2 left
💡 Hint
Trace the recursion from the last node back to the first.
✗ Incorrect
The recursive reversal flips the list order completely, making the last node the new head.
