Challenge - 5 Problems
Linked List Pop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after popping the last node?
Consider a singly linked list with nodes 10 -> 20 -> 30 -> null. After popping the last node, what is the resulting list?
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; } pop() { if (!this.head) return null; if (!this.head.next) { const val = this.head.value; this.head = null; return val; } let current = this.head; while (current.next && current.next.next) { current = current.next; } const val = current.next!.value; current.next = null; return val; } printList() { if (!this.head) return 'null'; let current = this.head; const values = []; while (current) { values.push(current.value); current = current.next; } return values.join(' -> ') + ' -> null'; } } const list = new LinkedList(); list.append(10); list.append(20); list.append(30); list.pop(); console.log(list.printList());
Attempts:
2 left
💡 Hint
Popping removes the last node from the list.
✗ Incorrect
The pop method removes the last node (30). The remaining list is 10 -> 20 -> null.
❓ Predict Output
intermediateWhat is the output after popping from a single-node list?
Given a linked list with a single node 5 -> null, what is the output after popping the last node and printing the list?
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; } pop() { if (!this.head) return null; if (!this.head.next) { const val = this.head.value; this.head = null; return val; } let current = this.head; while (current.next && current.next.next) { current = current.next; } const val = current.next!.value; current.next = null; return val; } printList() { if (!this.head) return 'null'; let current = this.head; const values = []; while (current) { values.push(current.value); current = current.next; } return values.join(' -> ') + ' -> null'; } } const list = new LinkedList(); list.append(5); list.pop(); console.log(list.printList());
Attempts:
2 left
💡 Hint
Popping the only node empties the list.
✗ Incorrect
Popping removes the only node, so the list becomes empty and printing shows 'null'.
🔧 Debug
advancedWhy does this pop method cause an error on empty list?
This pop method throws an error when called on an empty list. Identify the cause.
DSA Typescript
pop() {
let current = this.head;
while (current.next && current.next.next) {
current = current.next;
}
const val = current.next.value;
current.next = null;
return val;
}Attempts:
2 left
💡 Hint
Check what happens if the list is empty and current is assigned.
✗ Incorrect
If the list is empty, this.head is null, so current is null. Accessing current.next causes a TypeError.
🧠 Conceptual
advancedWhat is the time complexity of popping the last node in a singly linked list?
Given a singly linked list of n nodes, what is the time complexity of the pop operation that removes the last node?
Attempts:
2 left
💡 Hint
Think about how to find the node before the last one.
✗ Incorrect
To remove the last node, we must find the second last node by traversing the list, which takes O(n) time.
❓ Predict Output
expertWhat is the output after multiple pops on this list?
Given a linked list with nodes 1 -> 2 -> 3 -> 4 -> null, after calling pop() twice, what is the printed list?
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; } pop() { if (!this.head) return null; if (!this.head.next) { const val = this.head.value; this.head = null; return val; } let current = this.head; while (current.next && current.next.next) { current = current.next; } const val = current.next!.value; current.next = null; return val; } printList() { if (!this.head) return 'null'; let current = this.head; const values = []; while (current) { values.push(current.value); current = current.next; } return values.join(' -> ') + ' -> null'; } } const list = new LinkedList(); list.append(1); list.append(2); list.append(3); list.append(4); list.pop(); list.pop(); console.log(list.printList());
Attempts:
2 left
💡 Hint
Each pop removes the last node.
✗ Incorrect
First pop removes 4, second pop removes 3, leaving 1 -> 2 -> null.
