Challenge - 5 Problems
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting a middle value
What is the printed state of the doubly linked list after deleting the value 3?
DSA Typescript
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; tail: Node | null = null; append(value: number) { const newNode = new Node(value); if (!this.head) { this.head = this.tail = newNode; return; } this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } deleteByValue(value: number) { let current = this.head; while (current) { if (current.value === value) { if (current.prev) { current.prev.next = current.next; } else { this.head = current.next; } if (current.next) { current.next.prev = current.prev; } else { this.tail = current.prev; } break; } current = current.next; } } printList() { let current = this.head; const values = []; while (current) { values.push(current.value); current = current.next; } console.log(values.join(' -> ') + ' -> null'); } } const dll = new DoublyLinkedList(); dll.append(1); dll.append(2); dll.append(3); dll.append(4); dll.append(5); dll.deleteByValue(3); dll.printList();
Attempts:
2 left
💡 Hint
Think about how the links between nodes change when you remove a node in the middle.
✗ Incorrect
Deleting the node with value 3 removes it from the list. The previous node (2) now points to the next node (4), and the next node (4) points back to the previous node (2). The list becomes 1 -> 2 -> 4 -> 5 -> null.
❓ Predict Output
intermediateOutput after deleting the head node
What is the printed state of the doubly linked list after deleting the value 1 (head node)?
DSA Typescript
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; tail: Node | null = null; append(value: number) { const newNode = new Node(value); if (!this.head) { this.head = this.tail = newNode; return; } this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } deleteByValue(value: number) { let current = this.head; while (current) { if (current.value === value) { if (current.prev) { current.prev.next = current.next; } else { this.head = current.next; } if (current.next) { current.next.prev = current.prev; } else { this.tail = current.prev; } break; } current = current.next; } } printList() { let current = this.head; const values = []; while (current) { values.push(current.value); current = current.next; } console.log(values.join(' -> ') + ' -> null'); } } const dll = new DoublyLinkedList(); dll.append(1); dll.append(2); dll.append(3); dll.append(4); dll.append(5); dll.deleteByValue(1); dll.printList();
Attempts:
2 left
💡 Hint
Deleting the head means the next node becomes the new head.
✗ Incorrect
The head node with value 1 is removed. The next node with value 2 becomes the new head. The list is now 2 -> 3 -> 4 -> 5 -> null.
🔧 Debug
advancedIdentify the bug causing incorrect tail update
After deleting the last node by value, the tail pointer is not updated correctly. Which option shows the cause of this bug?
DSA Typescript
deleteByValue(value: number) {
let current = this.head;
while (current) {
if (current.value === value) {
if (current.prev) {
current.prev.next = current.next;
} else {
this.head = current.next;
}
if (current.next) {
current.next.prev = current.prev;
}
// Missing else block here
break;
}
current = current.next;
}
}Attempts:
2 left
💡 Hint
Think about what happens when deleting the last node and how tail should be updated.
✗ Incorrect
When deleting the last node, current.next is null. The code must update tail to current.prev in the else block. Missing this causes tail to remain unchanged, leading to incorrect tail pointer.
❓ Predict Output
advancedOutput after deleting a non-existent value
What is the printed state of the doubly linked list after attempting to delete the value 10 which is not in the list?
DSA Typescript
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; tail: Node | null = null; append(value: number) { const newNode = new Node(value); if (!this.head) { this.head = this.tail = newNode; return; } this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } deleteByValue(value: number) { let current = this.head; while (current) { if (current.value === value) { if (current.prev) { current.prev.next = current.next; } else { this.head = current.next; } if (current.next) { current.next.prev = current.prev; } else { this.tail = current.prev; } break; } current = current.next; } } printList() { let current = this.head; const values = []; while (current) { values.push(current.value); current = current.next; } console.log(values.join(' -> ') + ' -> null'); } } const dll = new DoublyLinkedList(); dll.append(1); dll.append(2); dll.append(3); dll.append(4); dll.append(5); dll.deleteByValue(10); dll.printList();
Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
✗ Incorrect
Since 10 is not in the list, no deletion occurs. The list remains 1 -> 2 -> 3 -> 4 -> 5 -> null.
🧠 Conceptual
expertWhy is updating both prev and next pointers necessary when deleting a node?
In a doubly linked list, when deleting a node by value, why must we update both the previous node's next pointer and the next node's prev pointer?
Attempts:
2 left
💡 Hint
Think about how nodes connect forward and backward in a doubly linked list.
✗ Incorrect
A doubly linked list has links in both directions. When deleting a node, both the previous node's next and the next node's prev pointers must be updated to keep the list connected correctly in both directions.
