Practice
addListsHelper when adding lists representing 724 and 564?Solution
Step 1: Trace addition from least significant digit
Adding 4 + 4 = 8, carry 0; next 2 + 6 = 8, carry 0; finally 7 + 5 = 12, carry 1.Step 2: Propagate carry backward
Carry 1 is returned from the most significant digit addition, indicating an overflow digit.Final Answer:
Option A -> Option AQuick Check:
724 + 564 = 1288, carry 1 at MSB addition [OK]
- Ignoring carry from most significant digit
- Off-by-one error in recursion base case
- Misaligning lists before addition
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def swapPairs(head: ListNode) -> ListNode:
def helper(node):
if not node or not node.next:
return node
first = node
second = node.next
first.next = helper(second.next)
second.next = first
return second
return helper(head)
# Input list: 1 -> 2 -> 3 -> null
What is the printed output after swapping pairs?Solution
Step 1: Trace helper on node with val=1
First = 1, Second = 2; recursively call helper on node with val=3.Step 2: Trace helper on node with val=3
Node 3 has no next, so return node 3 as is.Step 3: Complete first call
Set first.next = node 3, second.next = first (1), return second (2). Resulting list: 2 -> 1 -> 3.Final Answer:
Option C -> Option CQuick Check:
Output matches expected swapped pairs [OK]
- Swapping values instead of nodes
- Misplacing last node in odd-length list
Solution
Step 1: Identify auxiliary space usage
The algorithm modifies one input list in-place and only uses a few variables for carry and pointers.Step 2: Confirm no extra list or recursion stack
No new list nodes are created except possibly one if carry remains, which is constant extra space.Final Answer:
Option C -> Option CQuick Check:
In-place modification means constant extra space [OK]
- Confusing input list size with extra space
- Assuming recursion stack space applies here
- Thinking carry requires extra list storage
def deleteDuplicates(head):
if not head or not head.next:
return head
prev = None
current = head
while current and current.next:
if current.val == current.next.val:
val_to_remove = current.val
while current and current.val == val_to_remove:
current = current.next
if prev:
prev.next = current
else:
prev = current
current = current.next
return head
Solution
Step 1: Identify problem with head duplicates
Without a dummy node, if duplicates appear at the head, prev is None and cannot link to the new head after removal.Step 2: Analyze code behavior
Line 3 initializes prev to None instead of a dummy node, so when duplicates at the head are removed, the head pointer is not updated correctly, causing incorrect output.Final Answer:
Option A -> Option AQuick Check:
Dummy node needed to handle head duplicates [OK]
- Not using dummy head
- Incorrect prev updates
- Skipping null checks
Solution
Step 1: Understand cycle problem
Standard reversal assumes acyclic list; cycles cause infinite recursion or loops.Step 2: Modify algorithm
Tracking visited nodes prevents revisiting and infinite recursion by stopping when a node repeats.Step 3: Evaluate other options
Standard recursion (B) fails on cycles. Converting to array (C) is inefficient and not in-place. Limiting recursion depth (A) is arbitrary and unsafe.Final Answer:
Option A -> Option AQuick Check:
Visited set detects cycles and prevents infinite recursion [OK]
- Assuming input list is always acyclic
- Ignoring cycle detection in recursive reversal
