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 reverseList(head):
def helper(prev, curr):
if not curr:
return prev
next_temp = curr.next
curr.next = prev
return helper(curr, next_temp)
return helper(None, head)
head = ListNode(1, ListNode(2, ListNode(3)))
new_head = reverseList(head)
curr = new_head
while curr:
print(curr.val, end=' ')
curr = curr.next
print()Solution
Step 1: Trace helper calls
Initial call: helper(None, node1(1)) - next_temp = node2(2) - node1.next = None - call helper(node1, node2) Next call: helper(node1, node2) - next_temp = node3(3) - node2.next = node1 - call helper(node2, node3) Next call: helper(node2, node3) - next_temp = None - node3.next = node2 - call helper(node3, None) Next call: helper(node3, None) returns node3 as new head.Step 2: Print reversed list
Starting from node3, print values: 3 -> 2 -> 1Final Answer:
Option A -> Option AQuick Check:
Reversal correctly outputs 3 2 1 [OK]
- Confusing original order with reversed output
- Off-by-one in pointer updates
def reverseList(head):
def helper(prev, curr):
if not curr:
return prev
curr.next = prev
next_temp = curr.next
return helper(curr, next_temp)
return helper(None, head)Solution
Step 1: Analyze pointer updates
Line 5 assigns next_temp = curr.next after curr.next is set to prev, so next_temp points to prev, not the original next node.Step 2: Consequence of bug
Because next_temp is incorrect, recursion loses the rest of the list, causing incomplete reversal or infinite loops.Final Answer:
Option C -> Option CQuick Check:
Saving next node before pointer reversal is critical [OK]
- Swapping order of pointer updates
- Forgetting to set head.next to None in recursion
k places. Identify the line that contains a subtle bug that can cause the list to become cyclic or truncated incorrectly.Solution
Step 1: Understand the purpose of breaking the list
After rotation, the list must be broken at the new tail by settingslow.next = None.Step 2: Identify the bug
The original code setsslow.next = slow, which creates a cycle instead of breaking the list.Step 3: Correct fix
Change toslow.next = Noneto properly break the list.Final Answer:
Option B -> Option BQuick Check:
Correct break isslow.next = None[OK]
- Setting slow.next to itself or wrong node causing cycles
- Not handling k modulo length correctly
- Forgetting to make list circular before rotation
Solution
Step 1: Understand the limitation of the approach
The method copies data from the next node and deletes the next node, so it requires a next node.Step 2: Analyze tail node case
The tail node has no next node, so this approach cannot be applied to delete the tail node.Step 3: Consider alternatives
Deleting the tail node requires access to the head to update the previous node's next pointer.Final Answer:
Option D -> Option DQuick Check:
Tail node deletion requires head access, not possible with this method [OK]
- Assuming tail node can be deleted with same method
- Trying to create dummy next node to fix tail deletion
- Setting tail node value to None without pointer updates
