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 in that order. What is the final state of the list?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; insertAtBeginning(value: number) { const newNode = new Node(value); newNode.next = this.head; this.head = newNode; } toString(): string { let result = ''; let current = this.head; while (current) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const list = new LinkedList(); list.insertAtBeginning(10); list.insertAtBeginning(20); list.insertAtBeginning(30); console.log(list.toString());
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 builds backwards: 30 is head, then 20, then 10.
🧠 Conceptual
intermediateWhat happens to the head pointer after inserting at the beginning?
When you insert a new node at the beginning of a singly linked list, how does the head pointer change?
Attempts:
2 left
💡 Hint
Think about which node is the first after insertion.
✗ Incorrect
Inserting at the beginning means the new node becomes the first node, so head points to it.
🔧 Debug
advancedIdentify the error in this head insert code snippet
What error will this TypeScript code cause when inserting at the beginning of a linked list?
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
class LinkedList {
head: Node | null = null;
insertAtBeginning(value: number) {
const newNode = new Node(value);
newNode.next = null;
this.head = newNode;
}
}
Attempts:
2 left
💡 Hint
Check how the new node links to the existing list.
✗ Incorrect
The code sets newNode.next to null instead of the current head, so previous nodes are disconnected.
❓ Predict Output
advancedWhat is the output after multiple head inserts and one traversal?
Given the following code, what will be printed?
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
class LinkedList {
head: Node | null = null;
insertAtBeginning(value: number) {
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;
}
console.log(result + 'null');
}
}
const list = new LinkedList();
list.insertAtBeginning(5);
list.insertAtBeginning(15);
list.insertAtBeginning(25);
list.printList();
Attempts:
2 left
💡 Hint
New nodes become the head, so the list order reverses insertion order.
✗ Incorrect
Inserting at beginning adds nodes before the current head, so last inserted is first in list.
🧠 Conceptual
expertWhy is head insertion efficient in singly linked lists?
Why is inserting a node at the beginning (head) of a singly linked list considered efficient compared to inserting at the end?
Attempts:
2 left
💡 Hint
Think about how many nodes you must visit to insert at the beginning vs end.
✗ Incorrect
Inserting at the head only changes two pointers and does not require traversal, so it is O(1).
