Practice
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def deleteDuplicates(head):
if not head or not head.next:
return head
if head.val == head.next.val:
val_to_remove = head.val
while head and head.val == val_to_remove:
head = head.next
return deleteDuplicates(head)
else:
head.next = deleteDuplicates(head.next)
return head
Solution
Step 1: Trace initial call with head=1
1 != 2, so recurse on deleteDuplicates(2 -> 2 -> 3).Step 2: Trace call with head=2
2 == 2, remove all nodes with val=2 by advancing head until head.val != 2, resulting in head=3. Recurse on deleteDuplicates(3).Step 3: Trace call with head=3
Only one node, return head as is.Step 4: Reconstruct list
From step 1, head=1, head.next = result from step 3 (3), so final list is [1, 3].Final Answer:
Option C -> Option CQuick Check:
Duplicates 2 removed completely, 1 and 3 remain [OK]
- Leaving one duplicate node
- Not skipping all duplicates in while loop
- Incorrectly linking nodes after recursion
def deleteNode(node):
node.val = node.next.val
# Missing update of next pointer
What is the consequence of this bug?Solution
Step 1: Analyze the code's effect
Only the node's value is overwritten with the next node's value, but the next pointer is not updated.Step 2: Understand the consequence
Since next pointer is unchanged, the next node remains in the list, causing duplicate values and no actual deletion.Final Answer:
Option A -> Option AQuick Check:
Next pointer must be updated to remove next node [OK]
- Forgetting to update next pointer after copying value
- Assuming setting node to None deletes it
- Trying to delete tail node with this method
Solution
Step 1: Understand node comparison
Nodes must be compared by reference, not by value. Using '!=' compares values, not references, causing incorrect intersection detection.Step 2: Identify correct comparison operator
'is not' compares object identity, ensuring correct detection of intersection node.Final Answer:
Option A -> Option AQuick Check:
Using '!=' causes false positives or misses intersection [OK]
- Comparing node values instead of references
- Forgetting to handle empty lists
- Infinite loops due to pointer reset errors
Solution
Step 1: Understand input reuse constraint
Input lists must remain unmodified for reuse, so in-place reversal or padding that changes nodes is disallowed.Step 2: Identify approach preserving input integrity
Using stacks to simulate reverse traversal reads digits from least significant to most significant without modifying or copying the lists.Step 3: Compare alternatives
Reversing in-place breaks input reuse; copying is expensive; recursion without padding fails alignment.Final Answer:
Option A -> Option AQuick Check:
Stacks preserve input and allow correct addition [OK]
- Reversing input lists modifies them
- Copying inputs is costly and unnecessary
- Ignoring alignment leads to wrong sums
Solution
Step 1: Understand circular list implications
In a circular list, naive recursion never reaches a null node, causing infinite recursion.Step 2: Modify base case to detect cycle
Stopping recursion when next node is the head prevents infinite loops and preserves all nodes.Step 3: Compare alternatives
Using a visited set adds overhead; breaking cycle beforehand changes problem constraints; naive recursion fails.Final Answer:
Option A -> Option AQuick Check:
Base case adjusted for cycle detection prevents infinite recursion [OK]
- Ignoring cycle causes infinite recursion
- Trying naive recursion without cycle check
