Challenge - 5 Problems
Delete Node at Beginning Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting the first node in a singly linked list
What is the printed state of the linked list after deleting the first node?
DSA Typescript
class Node { data: number; next: Node | null; constructor(data: number) { this.data = data; this.next = null; } } class LinkedList { head: Node | null = null; insertAtEnd(data: number) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteAtBeginning() { if (!this.head) return; this.head = this.head.next; } printList() { let current = this.head; let result = ''; while (current) { result += current.data + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.insertAtEnd(10); list.insertAtEnd(20); list.insertAtEnd(30); list.deleteAtBeginning(); list.printList();
Attempts:
2 left
💡 Hint
Deleting the first node means the head moves to the next node.
✗ Incorrect
Initially, the list is 10 -> 20 -> 30 -> null. After deleting the first node (10), the head points to 20, so the list becomes 20 -> 30 -> null.
❓ Predict Output
intermediateOutput when deleting from an empty linked list
What is the output when deleteAtBeginning is called on an empty linked list?
DSA Typescript
class Node { data: number; next: Node | null; constructor(data: number) { this.data = data; this.next = null; } } class LinkedList { head: Node | null = null; deleteAtBeginning() { if (!this.head) return; this.head = this.head.next; } printList() { let current = this.head; let result = ''; while (current) { result += current.data + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.deleteAtBeginning(); list.printList();
Attempts:
2 left
💡 Hint
Deleting from empty list should not change anything.
✗ Incorrect
Since the list is empty (head is null), deleteAtBeginning does nothing. Printing the list shows 'null'.
🔧 Debug
advancedIdentify the error in deleteAtBeginning method
What error will occur when running this code and why?
DSA Typescript
class Node { data: number; next: Node | null; constructor(data: number) { this.data = data; this.next = null; } } class LinkedList { head: Node | null = null; deleteAtBeginning() { this.head = this.head.next; } } const list = new LinkedList(); list.deleteAtBeginning();
Attempts:
2 left
💡 Hint
Check what happens if head is null before accessing next.
✗ Incorrect
head is null initially, so accessing head.next causes a TypeError.
❓ Predict Output
advancedOutput after multiple deletions at beginning
What is the printed state of the linked list after deleting the first node twice?
DSA Typescript
class Node { data: number; next: Node | null; constructor(data: number) { this.data = data; this.next = null; } } class LinkedList { head: Node | null = null; insertAtEnd(data: number) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteAtBeginning() { if (!this.head) return; this.head = this.head.next; } printList() { let current = this.head; let result = ''; while (current) { result += current.data + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.insertAtEnd(5); list.insertAtEnd(15); list.insertAtEnd(25); list.deleteAtBeginning(); list.deleteAtBeginning(); list.printList();
Attempts:
2 left
💡 Hint
Each deletion removes the current head node.
✗ Incorrect
After first deletion, list is 15 -> 25 -> null. After second deletion, list is 25 -> null.
🧠 Conceptual
expertEffect of deleting the first node on list length
If a singly linked list has 7 nodes, what will be the length after deleting the first node?
Attempts:
2 left
💡 Hint
Deleting the first node removes exactly one node from the list.
✗ Incorrect
Deleting the first node reduces the list length by one, so from 7 to 6.
