Challenge - 5 Problems
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after deleting a middle value?
Consider a doubly linked list with nodes 1 <-> 2 <-> 3 <-> 4. We delete the node with value 3. What is the printed list after deletion?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; return; } this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } deleteByValue(value) { 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; } return; } current = current.next; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(1); dll.append(2); dll.append(3); dll.append(4); dll.deleteByValue(3); dll.printList();
Attempts:
2 left
💡 Hint
Think about how the links between nodes change when you remove a middle node.
✗ Incorrect
Deleting the node with value 3 removes it from the list. The previous node (2) now points to the next node (4), and 4's previous points to 2. The list becomes 1 -> 2 -> 4 -> null.
❓ Predict Output
intermediateWhat happens when deleting the head node?
Given a doubly linked list 10 <-> 20 <-> 30, if we delete the node with value 10 (the head), what is the printed list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; return; } this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } deleteByValue(value) { 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; } return; } current = current.next; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(10); dll.append(20); dll.append(30); dll.deleteByValue(10); dll.printList();
Attempts:
2 left
💡 Hint
Deleting the head means the next node becomes the new head.
✗ Incorrect
Removing the head node (10) updates the head pointer to the next node (20). The list becomes 20 -> 30 -> null.
❓ Predict Output
advancedWhat is the output after deleting the tail node?
For a doubly linked list 5 <-> 15 <-> 25, if we delete the node with value 25 (the tail), what is the printed list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; return; } this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } deleteByValue(value) { 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; } return; } current = current.next; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(5); dll.append(15); dll.append(25); dll.deleteByValue(25); dll.printList();
Attempts:
2 left
💡 Hint
Deleting the tail means the previous node becomes the new tail.
✗ Incorrect
Removing the tail node (25) updates the tail pointer to the previous node (15). The list becomes 5 -> 15 -> null.
❓ Predict Output
advancedWhat is the output when deleting a non-existent value?
Given a doubly linked list 7 <-> 14 <-> 21, if we try to delete the node with value 100 (which does not exist), what is the printed list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; return; } this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } deleteByValue(value) { 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; } return; } current = current.next; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(7); dll.append(14); dll.append(21); dll.deleteByValue(100); dll.printList();
Attempts:
2 left
💡 Hint
If the value is not found, the list stays the same.
✗ Incorrect
Since 100 is not in the list, no deletion happens. The list remains 7 -> 14 -> 21 -> null.
🧠 Conceptual
expertWhat is the time complexity of deleting a node by value in a doubly linked list?
Consider a doubly linked list with n nodes. What is the time complexity of deleting a node by its value when you do not have a direct reference to the node?
Attempts:
2 left
💡 Hint
Think about how you find the node before deleting it.
✗ Incorrect
Without a direct reference, you must traverse the list to find the node with the given value, which takes O(n) time. Deletion itself is O(1) once the node is found.
