Practice
1. You are given a singly linked list where each node contains an additional pointer that can point to any node in the list or null. The task is to create a deep copy of this list, preserving both the next and random pointer structure. Which approach guarantees an optimal solution with O(n) time and O(1) extra space?
easy
Solution
Step 1: Understand the problem constraints
The problem requires copying a complex linked list with random pointers efficiently.Step 2: Identify the optimal approach
Interleaving copied nodes within the original list allows assigning random pointers without extra space, then separating the lists restores the original and creates the copy.Final Answer:
Option A -> Option AQuick Check:
Interleaving nodes achieves O(n) time and O(1) space [OK]
Hint: Interleaving nodes avoids extra space for mapping [OK]
Common Mistakes:
- Assuming recursion is always optimal
- Using extra hash maps increases space complexity
- Trying multiple traversals without mapping fails random pointer assignment
2. What is the time complexity of the recursive approach that converts a binary number in a linked list to an integer by accumulating the value with bit shifts?
medium
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]
Hint: Bit shifts are O(1) operations, not O(log n) [OK]
Common Mistakes:
- Assuming bit shifts cost O(log n)
- Confusing recursion stack space with time complexity
- Thinking recursion causes repeated work
3. What is the space complexity of the optimal approach that copies a linked list with random pointers by weaving copied nodes into the original list and then separating them?
medium
Solution
Step 1: Identify auxiliary space usage
The optimal approach weaves copied nodes directly into the original list without using extra hash maps or recursion.Step 2: Confirm space complexity
Only new nodes are created, which is required output space, so auxiliary space is O(1).Final Answer:
Option D -> Option DQuick Check:
No extra data structures or recursion stack used [OK]
Hint: Weaving nodes avoids extra mapping space [OK]
Common Mistakes:
- Confusing output space with auxiliary space
- Assuming recursion is used and adds stack space
- Thinking random pointers require extra space
4. The following code attempts to reorder a linked list using the recursive approach. Identify the line containing the subtle bug that can cause infinite loops or cycles when traversing the reordered list.
def reorderList(head):
def helper(front, back):
if not back:
return True
if not helper(front, back.next):
return False
if front[0] == back or front[0].next == back:
# Missing termination here
return False
tmp = front[0].next
front[0].next = back
back.next = tmp
front[0] = tmp
return True
helper([head], head)
medium
Solution
Step 1: Identify termination condition
The condition checking if front meets back must terminate the list by setting back.next = None.Step 2: Locate missing termination
Line with 'if front[0] == back or front[0].next == back:' lacks 'back.next = None', causing cycles.Final Answer:
Option B -> Option BQuick Check:
Missing null termination leads to infinite traversal [OK]
Hint: Termination must set back.next = None to avoid cycles [OK]
Common Mistakes:
- Forgetting to null-terminate reordered list
- Misplacing pointer updates
- Incorrect base case handling
5. Suppose the problem is modified so that nodes can be reused multiple times (i.e., after reversing a group, nodes can appear again in subsequent groups). Which of the following changes to the algorithm correctly handles this scenario?
hard
Solution
Step 1: Understand node reuse implication
Reusing nodes means original nodes must remain unchanged or duplicated to appear multiple times.Step 2: Evaluate algorithm modifications
In-place reversal modifies nodes destructively, so creating new nodes for each group is necessary to preserve original nodes.Step 3: Assess other options
Resetting pointers (A) breaks list integrity; recursion with memoization (C) is complex and not standard; queue simulation (B) does not solve reversal reuse.Final Answer:
Option A -> Option AQuick Check:
Duplicating nodes preserves original list for reuse [OK]
Hint: Node reuse requires duplication, not in-place reversal [OK]
Common Mistakes:
- Trying to reuse nodes in place
- Ignoring list integrity
- Assuming recursion memoization solves reuse
