Practice
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def mergeTwoLists(l1, l2):
if not l1:
return l2
if not l2:
return l1
if l1.val > l2.val:
l1, l2 = l2, l1
head = l1
while l1 and l2:
prev = None
while l1 and l1.val <= l2.val:
prev = l1
l1 = l1.next
prev.next = l2
l1, l2 = l2, l1
return head
# Input lists:
# l1: 1 -> 3 -> 5
# l2: 2 -> 4
merged_head = mergeTwoLists(l1, l2)
What is the value of the node pointed to by prev after the first inner while loop iteration inside the outer while loop?Solution
Step 1: Trace the inner while loop with initial lists
Initially, l1 points to 1, l2 points to 2. The inner loop advances l1 while l1.val <= l2.val: - l1=1 (<=2), prev=1, l1 moves to 3 - l1=3 (>2), loop stopsStep 2: Identify prev after loop
Prev points to node with value 1 after the inner loop ends, which is the last node with value less than or equal to l2.val.Final Answer:
Option C -> Option CQuick Check:
Prev points to last l1 node ≤ current l2 node [OK]
- Assuming prev points to node with value 3 after first inner loop
- Confusing l1 and l2 pointers after swap
- Forgetting that prev updates inside inner loop
Solution
Step 1: Trace initial list and first iteration
Start with current at first node (1). current.next is also 1, so duplicate detected; remove second node by linking current.next to current.next.next (node with value 2).Step 2: Continue iteration
Now current.next is 2, which differs from current.val (1), so move current to next node (2). current.next is None, loop ends.Final Answer:
Option B -> Option BQuick Check:
Duplicates removed, final list is 1 -> 2 [OK]
- Forgetting to move current pointer after removal
def mergeTwoLists(l1, l2):
if not l1:
return l2
if not l2:
return l1
if l1.val > l2.val:
l1, l2 = l2, l1
head = l1
while l1 and l2:
prev = None
while l1 and l1.val <= l2.val:
prev = l1
# BUG: Missing l1 pointer advancement here
prev.next = l2
l1, l2 = l2, l1
return head
What is the bug in this code?Solution
Step 1: Identify the inner while loop behavior
The inner loop should advance l1 pointer to traverse nodes with values <= l2.val. Missing l1 = l1.next causes infinite loop.Step 2: Confirm impact of missing advancement
Without advancing l1, prev remains the same, loop never ends, causing infinite loop or crash.Final Answer:
Option D -> Option DQuick Check:
Missing l1 advancement inside inner loop causes infinite loop [OK]
- Confusing swap of l1 and l2 as bug
- Thinking return head is incorrect
- Assuming prev initialization causes error
Solution
Step 1: Identify missing dummy node
Without a dummy node, removing the head node causes pointer errors or incorrect list head.Step 2: Check recursion call and return
Recursion is called on head, but no dummy means no safe predecessor to update head pointer.Final Answer:
Option C -> Option CQuick Check:
Dummy node is essential to handle head removal safely [OK]
- Not using dummy node
- Incorrect recursion base case
- Returning wrong node after removal
Solution
Step 1: Understand forward order challenge
Digits stored most significant first require addition from tail to head to handle carry properly.Step 2: Reverse input lists
Reversing both lists converts them to reverse order, allowing reuse of the in-place addition algorithm.Step 3: Reverse result list
After addition, reverse the result to restore forward order.Final Answer:
Option D -> Option DQuick Check:
Reversing lists enables correct carry handling in forward order [OK]
- Trying to add forward order lists without reversing
- Assuming carry can be handled left-to-right easily
- Creating new lists unnecessarily
