Challenge - 5 Problems
Linked List Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Traversing a Linked List
What is the output of the following Java code that traverses and prints a singly linked list?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } public class Main { public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); Node current = head; while (current != null) { System.out.print(current.data + " -> "); current = current.next; } System.out.print("null"); } }
Attempts:
2 left
💡 Hint
Follow the linked list nodes from head until you reach null.
✗ Incorrect
The code creates a linked list with nodes containing 1, 2, and 3. It prints each node's data followed by ' -> ', ending with 'null'.
❓ Predict Output
intermediateOutput When Skipping a Node in Traversal
What is the output of this Java code that traverses a linked list but skips printing the second node?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } public class Main { public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); Node current = head; while (current != null) { if (current.data == 2) { current = current.next; continue; } System.out.print(current.data + " -> "); current = current.next; } System.out.print("null"); } }
Attempts:
2 left
💡 Hint
Notice the continue statement skips printing when data is 2.
✗ Incorrect
The code skips printing the node with data 2, so only nodes 1 and 3 are printed followed by 'null'.
🔧 Debug
advancedIdentify the Error in Linked List Traversal
What error will this Java code produce when trying to traverse and print a linked list?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } public class Main { public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); Node current = head; while (current.next != null) { System.out.print(current.data + " -> "); current = current.next; } System.out.print("null"); } }
Attempts:
2 left
💡 Hint
Check the loop condition carefully; it stops before the last node.
✗ Incorrect
The loop condition 'current.next != null' stops before printing the last node (3), so only 1 and 2 are printed before 'null'.
❓ Predict Output
advancedOutput of Recursive Linked List Print
What is the output of this recursive Java method that prints a linked list?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } public class Main { public static void printList(Node node) { if (node == null) { System.out.print("null"); return; } System.out.print(node.data + " -> "); printList(node.next); } public static void main(String[] args) { Node head = new Node(5); head.next = new Node(10); head.next.next = new Node(15); printList(head); } }
Attempts:
2 left
💡 Hint
The method prints current node then calls itself on next node.
✗ Incorrect
The recursive method prints each node's data followed by ' -> ', then prints 'null' at the end.
🧠 Conceptual
expertNumber of Nodes Printed After Partial Traversal
Given a linked list with nodes containing values 7, 14, 21, 28, 35, what is the number of nodes printed by this Java code snippet?
DSA Java
Node head = new Node(7); head.next = new Node(14); head.next.next = new Node(21); head.next.next.next = new Node(28); head.next.next.next.next = new Node(35); Node current = head.next.next; // start at node with 21 int count = 0; while (current != null) { System.out.print(current.data + " -> "); current = current.next; count++; } System.out.print("null"); // What is the value of count after this loop?
Attempts:
2 left
💡 Hint
Count nodes starting from the third node (value 21) to the end.
✗ Incorrect
Starting at node with value 21, the loop prints nodes 21, 28, and 35, so count is 3.
