Unit 6 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
m
and n
are the same?
HAPPY CASE
Input: 1 -> 2 -> 3 -> 4 -> 5, m = 2, n = 4
Output: 1 -> 4 -> 3 -> 2 -> 5
Explanation: Nodes between position 2 and 4 are reversed, leading to the sequence 4 -> 3 -> 2.
EDGE CASE
Input: 1 -> 2 -> 3, m = 1, n = 1
Output: 1 -> 2 -> 3
Explanation: As m and n are the same, no nodes are reversed, and the list remains unchanged.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
This problem is a variation of the classic linked list reversal but with a constraint to only reverse a subsection of the list.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use a temporary node to simplify edge cases and use pointers to reverse the specified section.
1) Create a temp node to simplify operations at the head.
2) Use pointers to navigate to the start of the section to reverse.
3) Reverse the nodes between m and n using pointer manipulation.
4) Connect the reversed section back into the list.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def reverse_between(head, m, n):
if not head or m == n:
return head
# Create a temporary head node to simplify edge cases where m is 1
temp_head = Node(0)
temp_head.next = head
prev = temp_head
# Step 1: Reach the node just before position m
for i in range(m - 1):
prev = prev.next
# `prev` now is the node just before m, and `start` will be the first node to reverse
start = prev.next
then = start.next
# Step 2: Reverse from m to n
for _ in range(n - m):
start.next = then.next # Remove `then` from the list
then.next = prev.next # Insert `then` at the beginning of the reversed section
prev.next = then # Move `prev` to point to `then` as the new start of the reversed section
then = start.next # Move `then` to the next node to be reversed
return temp_head.next
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N)
since we might need to traverse the entire list in the worst case.O(1)
as we are using a constant amount of space regardless of the input size.