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 the last node?
Consider a doubly linked list with nodes 1 <-> 2 <-> 3 <-> null. After deleting the last node, what is the printed list?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; } append(data) { 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; } deleteFromEnd() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } let curr = this.head; while (curr.next) { curr = curr.next; } curr.prev.next = 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(1); dll.append(2); dll.append(3); dll.deleteFromEnd(); dll.printList();
Attempts:
2 left
💡 Hint
Deleting from end removes the last node and updates the previous node's next pointer.
✗ Incorrect
The last node with value 3 is removed. The node with value 2 becomes the last node, so its next pointer is set to null.
❓ Predict Output
intermediateWhat happens when deleting from a single-node list?
Given a doubly linked list with a single node 5, what is the printed list after deleting from the end?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; } append(data) { 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; } deleteFromEnd() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } let curr = this.head; while (curr.next) { curr = curr.next; } curr.prev.next = 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(5); dll.deleteFromEnd(); dll.printList();
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
✗ Incorrect
Since the list has only one node, deleting from end sets head to null, resulting in an empty list.
🔧 Debug
advancedWhy does this deleteFromEnd method cause an error?
This deleteFromEnd method sometimes throws an error. Why?
```js
deleteFromEnd() {
if (!this.head) return;
let curr = this.head;
while (curr.next) {
curr = curr.next;
}
curr.prev.next = null;
}
```
Attempts:
2 left
💡 Hint
Think about what happens when the list has only one node.
✗ Incorrect
When the list has only one node, curr.prev is null. Trying to access curr.prev.next causes a TypeError.
❓ Predict Output
advancedWhat is the output after multiple deletions from end?
Starting with list 10 <-> 20 <-> 30 <-> 40 <-> null, after calling deleteFromEnd() twice, what is the printed list?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; } append(data) { 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; } deleteFromEnd() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } let curr = this.head; while (curr.next) { curr = curr.next; } curr.prev.next = 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.append(40); dll.deleteFromEnd(); dll.deleteFromEnd(); dll.printList();
Attempts:
2 left
💡 Hint
Each deleteFromEnd removes the last node.
✗ Incorrect
First deletion removes 40, second removes 30, leaving 10 -> 20 -> null.
🧠 Conceptual
expertWhat is the time complexity of deleting from the end in a doubly linked list without tail pointer?
If a doubly linked list does NOT maintain a tail pointer, what is the time complexity of deleting the last node?
Attempts:
2 left
💡 Hint
Without a tail pointer, you start at head and move forward to find the last node.
✗ Incorrect
Without a tail pointer, you must traverse all nodes to reach the last node, making deletion O(n).
