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 last node from doubly linked list
What is the printed state of the doubly linked list after deleting the last 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 = null; append(data: number) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let temp = this.head; while (temp.next) { temp = temp.next; } temp.next = newNode; newNode.prev = temp; } deleteFromEnd() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } let temp = this.head; while (temp.next) { temp = temp.next; } temp.prev!.next = null; } printList() { let temp = this.head; let result = ''; while (temp) { result += temp.data + ' -> '; temp = temp.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(10); dll.append(20); dll.append(30); dll.deleteFromEnd(); dll.printList();
Attempts:
2 left
💡 Hint
Think about what happens to the last node and its previous node when deleting from the end.
✗ Incorrect
The deleteFromEnd method removes the last node (30). After deletion, the list contains 10 and 20 nodes only.
❓ Predict Output
intermediateOutput after deleting last node from single-node doubly linked list
What is the printed state of the doubly linked list after deleting the last 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 = null; append(data: number) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let temp = this.head; while (temp.next) { temp = temp.next; } temp.next = newNode; newNode.prev = temp; } deleteFromEnd() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } let temp = this.head; while (temp.next) { temp = temp.next; } temp.prev!.next = null; } printList() { let temp = this.head; let result = ''; while (temp) { result += temp.data + ' -> '; temp = temp.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(100); dll.deleteFromEnd(); dll.printList();
Attempts:
2 left
💡 Hint
Deleting the last node when only one node exists should empty the list.
✗ Incorrect
The deleteFromEnd method sets head to null when only one node exists, so the list becomes empty.
🔧 Debug
advancedIdentify the error in deleteFromEnd method
What error will this code produce when deleting from an empty doubly linked list?
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 = null; deleteFromEnd() { let temp = this.head!; while (temp.next) { temp = temp.next; } temp.prev!.next = null; } } const dll = new DoublyLinkedList(); dll.deleteFromEnd();
Attempts:
2 left
💡 Hint
Consider what happens when head is null and you try to access its properties.
✗ Incorrect
The code uses non-null assertion (!) on head without checking if head is null, causing a TypeError when head is null.
❓ Predict Output
advancedOutput after multiple deletions from end
What is the printed state of the doubly linked list after deleting the last node twice?
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 = null; append(data: number) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let temp = this.head; while (temp.next) { temp = temp.next; } temp.next = newNode; newNode.prev = temp; } deleteFromEnd() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } let temp = this.head; while (temp.next) { temp = temp.next; } temp.prev!.next = null; } printList() { let temp = this.head; let result = ''; while (temp) { result += temp.data + ' -> '; temp = temp.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(5); dll.append(15); dll.append(25); dll.deleteFromEnd(); dll.deleteFromEnd(); dll.printList();
Attempts:
2 left
💡 Hint
Each deleteFromEnd removes the last node. After two deletions, only the first node remains.
✗ Incorrect
After deleting last node twice, nodes with values 25 and 15 are removed, leaving only 5.
🧠 Conceptual
expertWhy is prev pointer important in deleteFromEnd of doubly linked list?
Which statement best explains why the 'prev' pointer is used when deleting the last node in a doubly linked list?
Attempts:
2 left
💡 Hint
Think about how to remove the last node without traversing the entire list again.
✗ Incorrect
The 'prev' pointer points to the node before the last node, so we can set its next pointer to null to remove the last node.
