Challenge - 5 Problems
Delete Node at End Master
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 Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; append(value: number) { 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; } deleteAtEnd() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } let current = this.head; while (current.next && current.next.next) { current = current.next; } current.next = null; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; 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 last node when you delete it.
✗ Incorrect
The deleteAtEnd method removes the last node (30). So the list becomes 10 -> 20 -> null.
❓ 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 from a list with one node?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; append(value: number) { 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; } deleteAtEnd() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } let current = this.head; while (current.next && current.next.next) { current = current.next; } current.next = null; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; 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 one node, deleting the last node sets head to null, so the list is empty and prints null.
🔧 Debug
advancedIdentify the error in deleteAtEnd method for a single-node list
What error will this code produce when deleting the last node from a list with one node?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; append(value: number) { 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; } deleteAtEnd() { if (!this.head) return; let current = this.head; while (current.next && current.next.next) { current = current.next; } current.next = null; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.append(1); list.deleteAtEnd(); list.printList();
Attempts:
2 left
💡 Hint
Check the while loop condition when list has one node.
✗ Incorrect
When list has one node, current.next is null, so accessing current.next.next causes TypeError: Cannot read property 'next' of null.
❓ Predict Output
advancedOutput after deleting last node in a circular linked list
What is the printed state of the circular linked list after deleting the last node?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class CircularLinkedList { head: Node | null = null; append(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; newNode.next = this.head; return; } let current = this.head; while (current.next !== this.head) { current = current.next!; } current.next = newNode; newNode.next = this.head; } deleteAtEnd() { if (!this.head) return; if (this.head.next === this.head) { this.head = null; return; } let current = this.head; while (current.next && current.next.next !== this.head) { current = current.next; } current.next = this.head; } printList() { if (!this.head) { console.log('null'); return; } let current = this.head; let result = ''; do { result += current.value + ' -> '; current = current.next!; } while (current !== this.head); result += '(head)'; console.log(result); } } const list = new CircularLinkedList(); list.append(5); list.append(10); list.append(15); list.deleteAtEnd(); list.printList();
Attempts:
2 left
💡 Hint
Deleting last node means the node before head points to head.
✗ Incorrect
The last node (15) is removed, so the list is 5 -> 10 -> (head).
🧠 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 is slower than deleting the first node.
Attempts:
2 left
💡 Hint
Think about what pointers you need to update for each deletion.
✗ Incorrect
Deleting last node requires finding the node before last, which needs traversal. Deleting first node just moves head pointer.
