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: Understand the problem constraints
The problem requires checking palindrome property in a linked list without using extra space proportional to the list size, ruling out array conversion.Step 2: Analyze space and time complexity of options
Convert the linked list to an array and check if the array is a palindrome. uses O(n) extra space for array; Use a dynamic programming approach to store palindrome states for sublists. uses O(n^2) time and space; Use a recursive two-pointer technique that compares nodes from front and back simultaneously. uses O(n) time but O(n) space due to recursion stack; Use a greedy approach by comparing nodes pairwise from start and end without recursion or extra space. uses O(n) time and O(1) space by reversing the second half and comparing.Step 3: Identify optimal approach
Use a greedy approach by comparing nodes pairwise from start and end without recursion or extra space. is optimal with O(n) time and O(1) space, meeting constraints without recursion overhead.Final Answer:
Option D -> Option DQuick Check:
Greedy pairwise comparison with in-place reversal is optimal [OK]
- Assuming recursion uses no extra space
- Confusing DP with palindrome linked list
- Thinking array conversion is space optimal
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: Identify heap operations per node
Each node is pushed and popped exactly once from a heap of size ≤ k.Step 2: Calculate complexity
Heap push/pop cost is O(log k), total O(N log k) for N nodes.Final Answer:
Option D -> Option DQuick Check:
Heap size depends on k, not N, so O(N log k) [OK]
- Confusing N and k in log factor
- Assuming heap size grows with N
- Thinking sequential merges are optimal
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
