Practice
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
successor = None
def reverseN(head, n):
global successor
if n == 1:
successor = head.next
return head
last = reverseN(head.next, n - 1)
head.next.next = head
head.next = successor
return last
def reverseBetween(head, left, right):
if left == 1:
return reverseN(head, right)
head.next = reverseBetween(head.next, left - 1, right - 1)
return head
# Build list
nodes = [ListNode(i) for i in range(1, 5)]
for i in range(3):
nodes[i].next = nodes[i+1]
new_head = reverseBetween(nodes[0], 2, 3)
curr = new_head
while curr:
print(curr.val, end=' ')
curr = curr.next
Solution
Step 1: Trace reverseN for n=2 starting at node with val=2
reverseN reverses first 2 nodes starting at node 2: it recursively reverses node 3, then sets pointers to reverse links between 2 and 3.Step 2: Reconnect reversed sublist to original list
Node 1 points to reversed sublist head (node 3), and the tail of reversed sublist (node 2) points to node 4.Final Answer:
Option A -> Option AQuick Check:
Output matches expected partial reversal between positions 2 and 3 [OK]
- Assuming full list reversed
- Off-by-one error reversing wrong nodes
- Confusing node values with positions
Solution
Step 1: Identify loop condition
The loop runs only while l1 or l2 is not None, ignoring carry after both lists end.Step 2: Understand consequence
If carry remains after both lists are exhausted, it is never processed, losing the last digit.Final Answer:
Option A -> Option AQuick Check:
Loop must include carry to handle final digit [OK]
- Forgetting carry in loop condition
- Misplacing carry update inside loop
- Incorrectly updating prev pointer
Solution
Step 1: Identify operations performed
The algorithm copies the next node's value into the current node and updates the next pointer to skip the next node.Step 2: Analyze time complexity
These operations are constant time, no traversal or recursion is needed.Step 3: Consider the initial access to the node
However, if only the node to delete is given, no traversal is needed. But if only the value is given and node must be found, traversal is needed.Final Answer:
Option B -> Option BQuick Check:
Copy and pointer update are O(1) operations [OK]
- Assuming traversal from head is needed, making it O(n)
- Confusing recursion with iterative approach
- Thinking deletion involves multiple node copies
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: Analyze naive multiple calls approach
Running reverseBetween separately for each sublist can break list integrity because each call assumes original list structure, causing pointer conflicts.Step 2: Consider single traversal with multiple successor tracking
Modifying recursion to handle multiple sublists and track successors allows reversing all sublists in one pass, preserving list integrity and O(n) time.Step 3: Evaluate other options
Extracting and rebuilding is O(n) but more complex and error-prone; greedy swapping node values does not reverse nodes and breaks problem constraints.Final Answer:
Option A -> Option AQuick Check:
Single traversal with multiple successor pointers is optimal and correct [OK]
- Running separate reversals breaks list links
- Assuming value swaps suffice for reversal
- Rebuilding entire list is inefficient
