Hint: Next pointer must allow null for list end [OK]
Common Mistakes:
Not allowing next to be null
Using undefined instead of null
Mismatching property names
3. Given the following linked list code, what will be the printed output after calling pop() once?
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;
}
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();
medium
A. null
B. 10 -> 20 -> null
C. 20 -> 30 -> null
D. 10 -> 20 -> 30 -> null
Solution
Step 1: Understand list before pop
List after appending 10, 20, 30 is: 10 -> 20 -> 30 -> null.
Step 2: Trace pop operation
Pop removes last node (30), so list becomes 10 -> 20 -> null.
Final Answer:
10 -> 20 -> null -> Option B
Quick Check:
Pop removes last node [OK]
Hint: Pop removes last node, print shows remaining [OK]
Common Mistakes:
Assuming pop removes first node
Not updating next pointer to null
Printing old list without pop effect
4. Identify the bug in the following pop() method for a singly linked list and select the correct fix.
pop() {
if (!this.head) return null;
let current = this.head;
while (current.next) {
current = current.next;
}
const val = current.value;
current = null;
return val;
}
medium
A. Return current.next.value instead of current.value
B. Initialize current as null before the loop
C. The loop should stop at the second last node; set current.next = null to remove last node
D. No bug; code works correctly
Solution
Step 1: Analyze loop behavior
The loop moves current to the last node, but we need to stop at second last to remove last node.
Step 2: Fix removal of last node
Set current.next = null at second last node to remove last node properly.
Final Answer:
The loop should stop at the second last node; set current.next = null to remove last node -> Option C
Quick Check:
Stop at second last node to remove last [OK]
Hint: Stop at second last node to remove last [OK]
Common Mistakes:
Setting current = null does not remove node
Loop goes to last node instead of second last
Returning wrong node value
5. You have a singly linked list with nodes: 5 -> 15 -> 25 -> 35 -> null. You want to implement a pop() method that removes the last node and returns its value. However, if the list has only one node, pop() should remove it and set the list to empty. What will be the state of the list after calling pop() twice? Choose the correct printed output after these operations:
const list = new LinkedList();
list.append(5);
list.append(15);
list.append(25);
list.append(35);
list.pop();
list.pop();
list.print();
hard
A. null
B. 5 -> 15 -> 25 -> null
C. 25 -> 35 -> null
D. 5 -> 15 -> null
Solution
Step 1: Initial list and first pop
List starts as 5 -> 15 -> 25 -> 35 -> null. First pop removes 35, list becomes 5 -> 15 -> 25 -> null.
Step 2: Second pop removes last node 25
After second pop, list is 5 -> 15 -> null.
Final Answer:
5 -> 15 -> null -> Option D
Quick Check:
Two pops remove last two nodes [OK]
Hint: Each pop removes last node, so two pops remove last two nodes [OK]
Common Mistakes:
Removing nodes from front instead of end
Not updating list head when one node left
Assuming pop removes all nodes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.