A. Should check if poppedNode.next exists before assigning
B. Should decrement stack size counter after pop
C. Should set poppedNode.next to null after popping
D. No error, code works correctly
Solution
Step 1: Analyze pop method logic
Pop removes top node and returns its value correctly.
Step 2: Check for missing stack size update
Stack size counter is not decremented, causing size mismatch.
Final Answer:
Should decrement stack size counter after pop -> Option B
Quick Check:
Pop must update size counter [OK]
Hint: Always update size counter on push/pop [OK]
Common Mistakes:
Thinking next pointer must be cleared
Assuming code returns wrong value
Ignoring size counter updates
5. How would you modify a linked list stack to support a peek() method that returns the top element without removing it? Choose the correct implementation.
hard
A. peek() { return this.top ? this.top.value : null; }