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 the first node from a doubly linked list
What is the printed state of the doubly linked list after deleting the first node?
DSA Typescript
class Node { data: number; next: Node | null; prev: Node | null; constructor(data: number) { this.data = data; this.next = null; this.prev = null; } } class DoublyLinkedList { head: Node | null; constructor() { this.head = null; } append(data: number) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let curr = this.head; while (curr.next) { curr = curr.next; } curr.next = newNode; newNode.prev = curr; } deleteFromBeginning() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } this.head = this.head.next; this.head.prev = null; } printList() { let curr = this.head; let result = ''; while (curr) { result += curr.data + ' <-> '; curr = curr.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(10); dll.append(20); dll.append(30); dll.deleteFromBeginning(); dll.printList();
Attempts:
2 left
💡 Hint
Deleting from beginning removes the first node and updates head.
✗ Incorrect
Initially, the list is 10 <-> 20 <-> 30 <-> null. After deleting the first node (10), the head moves to 20, so the list becomes 20 <-> 30 <-> null.
🧠 Conceptual
intermediateEffect of deleting from an empty doubly linked list
What happens when you try to delete the first node from an empty doubly linked list?
Attempts:
2 left
💡 Hint
Check the condition when head is null before deletion.
✗ Incorrect
The deleteFromBeginning method checks if the list is empty (head is null) and returns immediately, so no error or change occurs.
❓ Predict Output
advancedOutput after deleting from a single-node doubly linked list
What is the printed state of the doubly linked list after deleting the first node when the list has only one node?
DSA Typescript
class Node { data: number; next: Node | null; prev: Node | null; constructor(data: number) { this.data = data; this.next = null; this.prev = null; } } class DoublyLinkedList { head: Node | null; constructor() { this.head = null; } append(data: number) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let curr = this.head; while (curr.next) { curr = curr.next; } curr.next = newNode; newNode.prev = curr; } deleteFromBeginning() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } this.head = this.head.next; this.head.prev = null; } printList() { let curr = this.head; let result = ''; while (curr) { result += curr.data + ' <-> '; curr = curr.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(100); dll.deleteFromBeginning(); dll.printList();
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
✗ Incorrect
Since the list has only one node, deleting it sets head to null, so printing shows 'null'.
🔧 Debug
advancedIdentify the bug in this deleteFromBeginning method
What error will this code produce when deleting the first node from a doubly linked list with multiple nodes?
DSA Typescript
deleteFromBeginning() {
if (!this.head) return;
this.head = this.head.next;
this.head.prev = null;
}Attempts:
2 left
💡 Hint
Consider what happens if the list has only one node.
✗ Incorrect
If the list has only one node, this.head.next is null, so this.head becomes null. Then trying to set this.head.prev causes a TypeError.
🚀 Application
expertNumber of nodes after multiple deletions from beginning
Given a doubly linked list with nodes 5 <-> 15 <-> 25 <-> 35 <-> 45 <-> null, after deleting from the beginning three times, how many nodes remain?
Attempts:
2 left
💡 Hint
Each deletion removes one node from the start.
✗ Incorrect
Starting with 5 nodes, deleting 3 nodes from the beginning leaves 2 nodes remaining.
