Challenge - 5 Problems
Head Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting nodes at the beginning?
Consider a singly linked list initially empty. We insert nodes with values 10, 20, and 30 at the beginning one by one. What is the final state of the list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtBeginning(value) { const newNode = new Node(value); newNode.next = this.head; this.head = newNode; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; return result; } } const list = new LinkedList(); list.insertAtBeginning(10); list.insertAtBeginning(20); list.insertAtBeginning(30); console.log(list.printList());
Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the newest node becomes the head.
✗ Incorrect
Each new node is added before the current head, so the list order is reversed from insertion order.
❓ Predict Output
intermediateWhat happens if we insert the same value multiple times at the beginning?
Using the same linked list insertAtBeginning method, insert the value 5 three times. What is the final list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtBeginning(value) { const newNode = new Node(value); newNode.next = this.head; this.head = newNode; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; return result; } } const list = new LinkedList(); list.insertAtBeginning(5); list.insertAtBeginning(5); list.insertAtBeginning(5); console.log(list.printList());
Attempts:
2 left
💡 Hint
Each insertion adds a new node at the front, even if values are the same.
✗ Incorrect
Nodes are distinct objects; inserting same value multiple times creates multiple nodes linked in order.
❓ Predict Output
advancedWhat is the output after inserting nodes at the beginning and then printing reversed?
After inserting 1, 2, 3 at the beginning of an empty linked list, we print the list in reverse order using recursion. What is the output?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtBeginning(value) { const newNode = new Node(value); newNode.next = this.head; this.head = newNode; } printReverse(node = this.head) { if (!node) return ''; return this.printReverse(node.next) + (node.next ? ' -> ' : '') + node.value; } } const list = new LinkedList(); list.insertAtBeginning(1); list.insertAtBeginning(2); list.insertAtBeginning(3); console.log(list.printReverse());
Attempts:
2 left
💡 Hint
The list is 3 -> 2 -> 1 normally; printing reverse reverses this order.
✗ Incorrect
Recursion prints from last node to first, so output is 1 -> 2 -> 3.
🧠 Conceptual
advancedWhy is inserting at the beginning of a singly linked list efficient?
Which reason best explains why inserting a node at the beginning (head) of a singly linked list is efficient?
Attempts:
2 left
💡 Hint
Think about how many nodes you need to touch to insert at the head.
✗ Incorrect
Inserting at the head only changes two pointers: the new node's next and the head pointer.
🔧 Debug
expertWhat error occurs when inserting at beginning with missing next assignment?
Consider this insertAtBeginning method missing the line that sets newNode.next = this.head. What error or output results after inserting 1, 2, 3 at the beginning and printing the list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtBeginning(value) { const newNode = new Node(value); // Missing: newNode.next = this.head; this.head = newNode; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; return result; } } const list = new LinkedList(); list.insertAtBeginning(1); list.insertAtBeginning(2); list.insertAtBeginning(3); console.log(list.printList());
Attempts:
2 left
💡 Hint
Without linking newNode.next to head, what happens to the list?
✗ Incorrect
Each new node becomes head but points to null, so list has only one node: the last inserted.
