Practice
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def oddEvenList(head):
if not head or not head.next:
return head
odd = head
even = head.next
even_head = even
while even and even.next:
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
odd.next = even_head
return head
# Input list: 1 -> 2 -> 3 -> 4
head = ListNode(1, ListNode(2, ListNode(3, ListNode(4))))
new_head = oddEvenList(head)
curr = new_head
res = []
while curr:
res.append(curr.val)
curr = curr.next
print(res)Solution
Step 1: Trace first iteration of while loop
odd=1, even=2, even.next=3; odd.next=3, odd moves to 3; even.next=4, even moves to 4.Step 2: Loop ends as even.next is null; link odd.next to even_head (2)
Final list order: 1 -> 3 -> 2 -> 4.Final Answer:
Option B -> Option BQuick Check:
Output matches expected odd-even rearrangement for input [1,2,3,4] [OK]
- Confusing node values with indices
- Off-by-one errors in pointer updates
Solution
Step 1: Understand the problem constraints
The problem requires reordering nodes in a specific pattern without extra space overhead.Step 2: Evaluate approaches for time and space
Finding the middle, reversing the second half, and merging achieves O(n) time and O(1) space, unlike storing nodes or DP which use extra space.Final Answer:
Option D -> Option DQuick Check:
Optimal approach uses middle finding and reversing [OK]
- Thinking greedy pointer swaps suffice
- Using extra arrays unnecessarily
- Confusing DP with linked list reorder
Solution
Step 1: Analyze time per node
Each node is processed once, performing a constant number of bit operations (shift and OR).Step 2: Consider recursion overhead
Recursion depth is n, but no repeated computations occur; each call does O(1) work.Final Answer:
Option C -> Option CQuick Check:
Linear traversal with constant work per node [OK]
- Assuming bit shifts cost O(log n)
- Confusing recursion stack space with time complexity
- Thinking recursion causes repeated work
class BrowserHistory:
def __init__(self, homepage: str):
self.back_stack = [homepage]
self.forward_stack = []
def visit(self, url: str) -> None:
self.back_stack.append(url)
# Missing forward_stack.clear() here
def back(self, steps: int) -> str:
while steps > 0 and len(self.back_stack) > 1:
self.forward_stack.append(self.back_stack.pop())
steps -= 1
return self.back_stack[-1]
def forward(self, steps: int) -> str:
while steps > 0 and self.forward_stack:
self.back_stack.append(self.forward_stack.pop())
steps -= 1
return self.back_stack[-1]
Solution
Step 1: Identify visit method behavior
Visit appends new URL but does not clear forward_stack, so forward history remains outdated.Step 2: Understand impact on forward navigation
Without clearing forward_stack, forward() returns stale pages that should have been discarded.Final Answer:
Option B -> Option BQuick Check:
Clearing forward_stack on visit is essential to maintain correct forward history [OK]
- Forgetting to clear forward stack on visit
- Incorrectly popping from back stack
- Mismanaging stack boundaries
Solution
Step 1: Analyze group reversal cost
Each group of k nodes is reversed in O(k) time.Step 2: Count total groups and total nodes processed
There are approximately n/k groups, so total time is O(k * n/k) = O(n).Final Answer:
Option B -> Option BQuick Check:
Each node is processed once during reversal [OK]
- Multiplying n by k incorrectly
- Assuming log k factor in reversal
- Ignoring that groups partition the list
