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?
Given the linked list and the pop operation below, what will be the printed linked list after popping the last node?
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; } 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.next) { current = current.next; } const val = current.next.value; current.next = null; return val; } print() { 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.pop(); list.print();
Attempts:
2 left
💡 Hint
Remember that pop removes the last node from the linked list.
✗ Incorrect
The pop method removes the last node (30) from the list. After popping, the list contains 10 and 20 nodes only.
❓ Predict Output
intermediateWhat is the output after popping from a single-node list?
What will be the printed linked list after popping the only node 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; } 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.next) { current = current.next; } const val = current.next.value; current.next = null; return val; } print() { 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.pop(); list.print();
Attempts:
2 left
💡 Hint
Popping the only node should leave the list empty.
✗ Incorrect
After popping the only node (100), the list becomes empty, so printing shows 'null'.
❓ Predict Output
advancedWhat is the output after multiple pops on a multi-node list?
Consider the following code. What will be the printed linked list after popping twice?
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; } 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.next) { current = current.next; } const val = current.next.value; current.next = null; return val; } print() { 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.append(2); list.append(3); list.append(4); list.pop(); list.pop(); list.print();
Attempts:
2 left
💡 Hint
Each pop removes the last node.
✗ Incorrect
After popping twice, nodes 4 and 3 are removed, leaving 1 and 2.
❓ Predict Output
advancedWhat error occurs when popping from an empty list?
What happens when pop is called on an empty linked list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } 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.next) { current = current.next; } const val = current.next.value; current.next = null; return val; } print() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); const popped = list.pop(); console.log(popped);
Attempts:
2 left
💡 Hint
Check the pop method's first condition.
✗ Incorrect
The pop method returns null if the list is empty, so console.log prints null.
🧠 Conceptual
expertHow many nodes remain after popping all nodes one by one?
If you start with a linked list of 5 nodes and pop nodes one by one until the list is empty, how many nodes remain after the last pop?
Attempts:
2 left
💡 Hint
Popping removes one node each time until none remain.
✗ Incorrect
Popping all nodes removes them all, so zero nodes remain at the end.
