Practice
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def deleteNode(node):
node.val = node.next.val
node.next = node.next.next
n4 = ListNode(9)
n3 = ListNode(1, n4)
n2 = ListNode(5, n3)
n1 = ListNode(4, n2)
deleteNode(n2)
curr = n1
while curr:
print(curr.val, end=' -> ' if curr.next else '\n')
curr = curr.next
Solution
Step 1: Trace deleteNode on node with value 5
Copy next node's value (1) into current node (5), so node.val becomes 1. Then update node.next to skip next node, pointing to node with value 9.Step 2: Print the list after deletion
List is now 4 -> 1 -> 9, as the original node with 5 is replaced by 1 and next points to 9.Final Answer:
Option C -> Option CQuick Check:
Node 5 replaced by 1, next pointer updated correctly [OK]
- Forgetting to update next pointer, causing duplicates
- Printing original list without changes
- Confusing node values after deletion
Solution
Step 1: Trace pointers through lists
pA starts at 1, pB at 9. They advance until pA reaches 4, pB reaches 4 (intersection node). Since nodes are shared, pA is pB at node with value 4.Step 2: Confirm return value
The function returns the intersection node, which has value 4.Final Answer:
Option B -> Option BQuick Check:
Intersection node value is 4 as expected [OK]
- Returning node value instead of node reference
- Confusing next pointers
- Off-by-one in pointer switching
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 node reuse constraints
Reusing nodes multiple times in a linked list is impossible without creating new nodes because each node can only have one next pointer.Step 2: Identify correct approach
To allow duplicates, the algorithm must create new nodes for each occurrence, which recursion can do naturally by creating new nodes on each call.Final Answer:
Option B -> Option BQuick Check:
Recursive merge with new nodes handles duplicates safely [OK]
- Trying to reuse nodes by resetting pointers causes cycles
- Assuming in-place merge can handle duplicates without new nodes
- Not realizing linked list nodes have single next pointer
k can be negative, indicating rotation to the left by |k| places. Which modification to the optimal algorithm correctly handles negative k values?Solution
Step 1: Understand negative rotation meaning
Negativekmeans rotate left by|k|, which is equivalent to rotating right bylength - |k|.Step 2: Normalize
Usingkto positive equivalentk = (k % length + length) % lengthconverts negativekto correct positive rotation count.Final Answer:
Option C -> Option CQuick Check:
Normalization handles negative rotations correctly [OK]
- Taking absolute value loses rotation direction
- Ignoring negative k leads to wrong output
- Reversing list unnecessarily complicates solution
