Challenge - 5 Problems
Delete Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after deleting the first node?
Consider a singly linked list with nodes containing values 10 -> 20 -> 30 -> null. After deleting the node at the beginning, what will be the printed list?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteAtBeginning() { if (!this.head) return; this.head = this.head.next; } printList() { let current = this.head; let result = ''; while (current) { result += current.data + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.append(10); list.append(20); list.append(30); list.deleteAtBeginning(); list.printList();
Attempts:
2 left
💡 Hint
Deleting the first node means the head moves to the next node.
✗ Incorrect
Initially, the list is 10 -> 20 -> 30 -> null. Deleting the first node removes 10, so the new head is 20, resulting in 20 -> 30 -> null.
❓ Predict Output
intermediateWhat happens when deleting from an empty list?
Given an empty singly linked list, what will be the output after calling deleteAtBeginning() and then printing the list?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } deleteAtBeginning() { if (!this.head) return; this.head = this.head.next; } printList() { let current = this.head; let result = ''; while (current) { result += current.data + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.deleteAtBeginning(); list.printList();
Attempts:
2 left
💡 Hint
Deleting from empty list should not change anything.
✗ Incorrect
Since the list is empty (head is null), deleteAtBeginning does nothing. Printing the list shows 'null'.
❓ Predict Output
advancedWhat is the output after multiple deletions at beginning?
Starting with a linked list 5 -> 15 -> 25 -> 35 -> null, after calling deleteAtBeginning() twice and then printing, what is the output?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } append(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } deleteAtBeginning() { if (!this.head) return; this.head = this.head.next; } printList() { let current = this.head; let result = ''; while (current) { result += current.data + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.append(5); list.append(15); list.append(25); list.append(35); list.deleteAtBeginning(); list.deleteAtBeginning(); list.printList();
Attempts:
2 left
💡 Hint
Each deletion removes the current head node.
✗ Incorrect
First deletion removes 5, second removes 15, so the list starts at 25 now.
🧠 Conceptual
advancedWhat is the time complexity of deleting the first node in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity of deleting the node at the beginning?
Attempts:
2 left
💡 Hint
Deleting the first node only changes the head pointer.
✗ Incorrect
Deleting the first node requires updating the head pointer to the next node, which is done in constant time.
🔧 Debug
expertIdentify the error in this deleteAtBeginning method
What error will this code produce when deleteAtBeginning is called on a non-empty list?
class LinkedList {
constructor() {
this.head = null;
}
deleteAtBeginning() {
this.head = this.head.next.next;
}
}
Attempts:
2 left
💡 Hint
Check what happens if head.next is null.
✗ Incorrect
If the list has only one node, head.next is null, so accessing head.next.next causes a TypeError.
