Challenge - 5 Problems
Delete Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting a middle node by value
What is the printed linked list after deleting the node with value 3?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; append(value: number) { 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; } deleteByValue(value: number) { if (!this.head) return; if (this.head.value === value) { this.head = this.head.next; return; } let current = this.head; while (current.next && current.next.value !== value) { current = current.next; } if (current.next && current.next.value === value) { current.next = current.next.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.append(1); list.append(2); list.append(3); list.append(4); list.deleteByValue(3); list.printList();
Attempts:
2 left
💡 Hint
Think about how the node with value 3 is removed and how the links are updated.
✗ Incorrect
The node with value 3 is in the middle. The deleteByValue method skips it by linking the node with value 2 directly to the node with value 4. So the list becomes 1 -> 2 -> 4 -> null.
❓ Predict Output
intermediateOutput after deleting the head node by value
What is the printed linked list after deleting the node with value 1 (head node)?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; append(value: number) { 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; } deleteByValue(value: number) { if (!this.head) return; if (this.head.value === value) { this.head = this.head.next; return; } let current = this.head; while (current.next && current.next.value !== value) { current = current.next; } if (current.next && current.next.value === value) { current.next = current.next.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.append(1); list.append(2); list.append(3); list.deleteByValue(1); list.printList();
Attempts:
2 left
💡 Hint
Deleting the head means the head pointer moves to the next node.
✗ Incorrect
The head node with value 1 is removed by setting head to head.next, so the list starts at 2 and continues to 3.
🔧 Debug
advancedIdentify the bug preventing deletion of head node in deleteByValue
This deleteByValue method has a bug when deleting the head node. What is the bug?
DSA Typescript
deleteByValue(value: number) {
if (!this.head) return;
let current = this.head;
while (current.next && current.next.value !== value) {
current = current.next;
}
if (current.next && current.next.value === value) {
current.next = current.next.next;
}
}Attempts:
2 left
💡 Hint
Consider what happens if the node to delete is the first node.
✗ Incorrect
The method does not handle deleting the head node separately. If the head node has the value to delete, the loop traverses the list but does not delete it.
🧠 Conceptual
advancedEffect of deleting a non-existent value
What happens to the linked list if deleteByValue is called with a value not present in the list?
Attempts:
2 left
💡 Hint
Think about how the method searches for the node to delete.
✗ Incorrect
If no node with the given value is found, the method does not modify any links, so the list stays the same.
🚀 Application
expertResulting list after multiple deletions by value
Given the initial list 5 -> 10 -> 15 -> 20 -> 25 -> null, what is the list after deleting nodes with values 10, 25, and 5 in that order?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; append(value: number) { 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; } deleteByValue(value: number) { if (!this.head) return; if (this.head.value === value) { this.head = this.head.next; return; } let current = this.head; while (current.next && current.next.value !== value) { current = current.next; } if (current.next && current.next.value === value) { current.next = current.next.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(); [5, 10, 15, 20, 25].forEach(v => list.append(v)); list.deleteByValue(10); list.deleteByValue(25); list.deleteByValue(5); list.printList();
Attempts:
2 left
💡 Hint
Delete nodes one by one and update the list each time.
✗ Incorrect
After deleting 10, list is 5 -> 15 -> 20 -> 25 -> null. After deleting 25, list is 5 -> 15 -> 20 -> null. After deleting 5 (head), list is 15 -> 20 -> null.
