Challenge - 5 Problems
Linked List Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this linked list traversal code?
Consider a singly linked list with nodes containing values 10, 20, 30 in that order. What will be printed after running the traversal code below?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } const head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); let current = head; let output = ''; while (current !== null) { output += current.value + ' -> '; current = current.next; } output += 'null'; console.log(output);
Attempts:
2 left
💡 Hint
Think about how the traversal moves from the head node to the next until it reaches null.
✗ Incorrect
The code starts at the head node (value 10) and moves through each node using the next pointer, appending each value followed by ' -> '. It stops when current is null, then adds 'null' at the end.
❓ Predict Output
intermediateWhat will this code print after traversing the linked list?
Given a linked list with nodes 5, 15, 25, what is the printed output of the following traversal code?
DSA Javascript
function printList(head) {
let current = head;
let result = '';
while (current) {
result += current.value + ' -> ';
current = current.next;
}
result += 'null';
console.log(result);
}
const node1 = { value: 5, next: null };
const node2 = { value: 15, next: null };
const node3 = { value: 25, next: null };
node1.next = node2;
node2.next = node3;
printList(node1);Attempts:
2 left
💡 Hint
The traversal starts at the first node and moves forward until no next node exists.
✗ Incorrect
The function prints each node's value followed by ' -> ', ending with 'null'. It visits nodes in the order 5, 15, 25.
🧠 Conceptual
advancedWhat is the time complexity of traversing a linked list with n nodes?
If you traverse a singly linked list from head to tail, visiting each node exactly once, what is the time complexity in terms of n, the number of nodes?
Attempts:
2 left
💡 Hint
Think about how many nodes you visit and how many steps it takes per node.
✗ Incorrect
Each node is visited once, so the total steps grow linearly with n, making it O(n).
❓ Predict Output
advancedWhat is the output of this code that prints a linked list backwards using recursion?
Given the linked list 1 -> 2 -> 3 -> null, what will the following recursive function print?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } const head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); function printReverse(node) { if (node === null) return; printReverse(node.next); process.stdout.write(node.value + ' -> '); } printReverse(head); process.stdout.write('null\n');
Attempts:
2 left
💡 Hint
The function calls itself before printing the current node's value.
✗ Incorrect
The recursion goes to the end of the list first, then prints values on the way back, reversing the order.
🔧 Debug
expertWhy does this linked list traversal code cause an infinite loop?
Examine the code below. It is supposed to print all node values in the linked list 7 -> 14 -> 21 -> null, but it never stops. What is the cause?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } const head = new Node(7); head.next = new Node(14); head.next.next = new Node(21); let current = head; while (current !== null) { console.log(current.value); // Missing update of current pointer here current = current.next; }
Attempts:
2 left
💡 Hint
Check if the loop moves forward through the list.
✗ Incorrect
Without updating current to current.next inside the loop, current never changes, so the loop never ends.
