Practice
Solution
Step 1: Understand the problem constraints
The digits are stored in forward order, so addition must start from the most significant digit. Direct addition from head to tail requires alignment of list lengths.Step 2: Identify the approach that handles alignment and carry correctly
Padding the shorter list ensures both lists have equal length. Recursively adding nodes from the most significant digit allows carry to propagate backward naturally without reversing or extra space.Final Answer:
Option C -> Option CQuick Check:
Padding + recursion handles carry and alignment correctly [OK]
- Reversing lists modifies inputs unexpectedly
- Greedy addition fails due to carry propagation
- Converting to integers causes overflow or inefficiency
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: Understand the problem constraints
The problem requires reversing a singly linked list in-place, which means changing the next pointers without extra data structures.Step 2: Evaluate approaches
The brute force recursive reversal (A) uses recursion but reverses pointers on the return path, risking stack overflow and extra space. The greedy approach (C) swaps values, which is not pointer reversal and may not be allowed. Dynamic programming (D) is not applicable here. The tail-recursive helper function (B) reverses pointers during recursion descent, maintaining O(n) time and O(n) space due to recursion stack, and is optimal.Final Answer:
Option D -> Option DQuick Check:
Tail recursion with prev and curr pointers ensures correct reversal [OK]
- Confusing value swapping with pointer reversal
- Using naive recursion causing stack overflow
Solution
Step 1: Understand tail pointer update
After merging, tail must move to the end of merged list to link next merges correctly.Step 2: Identify bug in tail advancement loop
The loop 'while tail.next: tail = tail.next' moves tail forward, but if tail.next is null, loop stops. However, if tail.next is not updated properly or merged list ends with a cycle, this can cause infinite loop or skip last node.Final Answer:
Option D -> Option DQuick Check:
Incorrect tail advancement causes malformed list or infinite loop [OK]
- Not breaking list properly in split causing cycles
- Not updating tail after merge causing truncated list
- Incorrectly assigning right sublist in split
Solution
Step 1: Understand node reuse implications
If nodes can be reused multiple times, re-linking existing nodes in merge can create cycles or corrupt list structure.Step 2: Modify merge to create new nodes
Creating new nodes during merge avoids modifying shared nodes, preventing cycles and preserving correctness.Step 3: Evaluate other options
Tracking visited nodes adds overhead and complexity; recursion does not solve reuse issues; no modification risks infinite loops.Final Answer:
Option C -> Option CQuick Check:
Creating new nodes ensures safe merges with reused nodes [OK]
- Assuming existing merge works with reused nodes
- Trying to detect cycles dynamically instead of preventing
- Switching to recursion without addressing reuse
