Challenge - 5 Problems
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 node by value in a singly linked list?
Consider a singly linked list with nodes containing values 1, 2, 3, 4, 5 in order. We delete the node with value 3. What is the printed linked list after deletion?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteByValue(value) { if (!this.head) return; if (this.head.value === value) { this.head = this.head.next; return; } let current = this.head; while (current.next && current.next.value !== value) { current = current.next; } if (current.next && current.next.value === value) { current.next = current.next.next; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); [1, 2, 3, 4, 5].forEach(v => list.append(v)); list.deleteByValue(3); list.printList();
Attempts:
2 left
💡 Hint
Remember to unlink the node with the target value and connect the previous node to the next node.
✗ Incorrect
The node with value 3 is removed by linking the node with value 2 directly to the node with value 4. The list after deletion is 1 -> 2 -> 4 -> 5 -> null.
❓ Predict Output
intermediateWhat happens when deleting a non-existent value from a singly linked list?
Given a singly linked list with values 10, 20, 30, 40, 50, what is the output after attempting to delete the value 60 (which is not in the list)?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteByValue(value) { if (!this.head) return; if (this.head.value === value) { this.head = this.head.next; return; } let current = this.head; while (current.next && current.next.value !== value) { current = current.next; } if (current.next && current.next.value === value) { current.next = current.next.next; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); [10, 20, 30, 40, 50].forEach(v => list.append(v)); list.deleteByValue(60); list.printList();
Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
✗ Incorrect
Since 60 is not in the list, no node is deleted. The list remains the same.
🔧 Debug
advancedWhy does this deleteByValue method fail to delete the last node?
The following code tries to delete a node by value from a singly linked list. It works for all nodes except the last one. What is the bug?
DSA Javascript
deleteByValue(value) {
let current = this.head;
while (current.next.value !== value) {
current = current.next;
}
current.next = current.next.next;
}Attempts:
2 left
💡 Hint
Check for null before accessing properties of next node.
✗ Incorrect
If the value to delete is in the last node, current.next is the last node, so current.next.next is null. Accessing current.next.value without checking if current.next exists causes an error.
❓ Predict Output
advancedWhat is the output after deleting the head node by value?
Given a singly linked list with values 7, 14, 21, 28, what is the printed list after deleting the node with value 7?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteByValue(value) { if (!this.head) return; if (this.head.value === value) { this.head = this.head.next; return; } let current = this.head; while (current.next && current.next.value !== value) { current = current.next; } if (current.next && current.next.value === value) { current.next = current.next.next; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); [7, 14, 21, 28].forEach(v => list.append(v)); list.deleteByValue(7); list.printList();
Attempts:
2 left
💡 Hint
Deleting the head means moving the head pointer to the next node.
✗ Incorrect
The head node with value 7 is removed by setting head to head.next, resulting in list starting at 14.
🧠 Conceptual
expertWhat is the time complexity of deleting a node by value in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity of deleting a node by value in the worst case?
Attempts:
2 left
💡 Hint
Think about how many nodes you might need to check before finding the target.
✗ Incorrect
In the worst case, the node to delete is at the end or not present, so we traverse all n nodes, making it O(n).
