Challenge - 5 Problems
Singly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of the linked list after these insertions?
Consider a singly linked list where you insert nodes with values 10, 20, and 30 at the end. What will be the printed linked list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtEnd(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.insertAtEnd(10); list.insertAtEnd(20); list.insertAtEnd(30); list.printList();
Attempts:
2 left
💡 Hint
Think about how nodes are added at the end and how the list is printed from head to tail.
✗ Incorrect
Nodes are inserted at the end, so the list grows as 10, then 10->20, then 10->20->30. Printing starts from head and follows next pointers until null.
🧠 Conceptual
intermediateWhat happens when you insert a node into an empty singly linked list?
When you insert a node into an empty singly linked list, what is the correct update to the list?
Attempts:
2 left
💡 Hint
Think about what the head pointer should reference when the list is empty.
✗ Incorrect
In an empty list, head is null. Inserting a node means head must point to this new node, making it the first element.
🔧 Debug
advancedWhy does this linked list insertion code cause an infinite loop?
Find the bug causing an infinite loop in this insertion code snippet:
DSA Javascript
insertAtEnd(value) {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = newNode;
}Attempts:
2 left
💡 Hint
Check how the pointer moves inside the while loop.
✗ Incorrect
Inside the loop, 'current = current;' does not advance the pointer, so the loop never ends.
❓ Predict Output
advancedWhat is the output after deleting the head node from this list?
Given a singly linked list with nodes 5 -> 15 -> 25 -> null, what is the printed list after deleting the head node?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtEnd(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteHead() { if (!this.head) return; this.head = this.head.next; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.insertAtEnd(5); list.insertAtEnd(15); list.insertAtEnd(25); list.deleteHead(); list.printList();
Attempts:
2 left
💡 Hint
Deleting the head means the second node becomes the new head.
✗ Incorrect
After deleting the head (5), the head points to 15, so the list is 15 -> 25 -> null.
🧠 Conceptual
expertHow many nodes are in the list after these operations?
Start with an empty singly linked list. Insert 3 nodes with values 1, 2, 3 at the end. Then delete the head node twice. How many nodes remain in the list?
Attempts:
2 left
💡 Hint
Count nodes after each operation carefully.
✗ Incorrect
After inserting 3 nodes, list has 3 nodes. Deleting head twice removes first two nodes, leaving 1 node.
