Challenge - 5 Problems
Delete Node at End Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting the last node in a singly linked list
What is the printed state of the linked list after deleting the last node?
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; } deleteAtEnd() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } let current = this.head; while (current.next.next) { current = current.next; } current.next = null; } 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.deleteAtEnd(); list.printList();
Attempts:
2 left
💡 Hint
Think about what happens to the second last node's next pointer when the last node is deleted.
✗ Incorrect
The deleteAtEnd method removes the last node by setting the next pointer of the second last node to null. So the list after deletion contains 10 and 20 nodes only.
❓ Predict Output
intermediateOutput after deleting the last node from a single-node list
What is the printed state of the linked list after deleting the last node when the list has only one node?
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; } deleteAtEnd() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } let current = this.head; while (current.next.next) { current = current.next; } current.next = null; } 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(100); list.deleteAtEnd(); list.printList();
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
✗ Incorrect
When the list has only one node, deleting the last node sets head to null, so the list becomes empty.
🔧 Debug
advancedIdentify the error in deleteAtEnd method
What error will this code produce when deleting the last node from a list with two nodes?
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; } deleteAtEnd() { if (!this.head) return; let current = this.head; while (current.next.next) { current = current.next; } current.next = null; } 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(1); list.append(2); list.deleteAtEnd(); list.printList();
Attempts:
2 left
💡 Hint
Check if the code handles the case when the list has exactly two nodes.
✗ Incorrect
The code correctly deletes the last node by setting the next of the first node to null when the list has two nodes.
❓ Predict Output
advancedOutput after multiple deletions at end
What is the printed state of the linked list after deleting the last node twice in a list with three nodes?
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; } deleteAtEnd() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } let current = this.head; while (current.next.next) { current = current.next; } current.next = null; } 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(10); list.append(15); list.deleteAtEnd(); list.deleteAtEnd(); list.printList();
Attempts:
2 left
💡 Hint
Each deleteAtEnd removes one node from the end.
✗ Incorrect
After two deletions, the list has only the first node left with data 5.
🧠 Conceptual
expertWhy is deleting the last node in a singly linked list less efficient than deleting the first node?
Choose the best explanation for why deleting the last node in a singly linked list takes more time than deleting the first node.
Attempts:
2 left
💡 Hint
Think about how you find the node before the last node in a singly linked list.
✗ Incorrect
In singly linked lists, you cannot directly access the previous node, so to delete the last node, you must traverse from the head to find the second last node. Deleting the first node is direct by moving the head pointer.
