TIP102 Unit 5 Session 2 Standard (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?
None
.HAPPY CASE
Input: head = Node("Daisy") -> Node("Mario") -> Node("Toad") -> Node("Mario"), racer = "Mario"
Output: head = Node("Daisy") -> Node("Toad") -> Node("Mario")
Explanation: The first node with value "Mario" is removed from the list.
EDGE CASE
Input: head = Node("Daisy") -> Node("Mario") -> Node("Toad"), racer = "Yoshi"
Output: head = Node("Daisy") -> Node("Mario") -> Node("Toad")
Explanation: Since "Yoshi" is not found in the list, the original list is returned unchanged.
EDGE CASE
Input: head = None, racer = "Mario"
Output: None
Explanation: When the linked list is empty, the function returns `None`.
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.
For Linked List problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the linked list to find the node with the specified value and remove it by updating the next
pointers.
1) If the head is `None`, return `None`.
2) If the head node's value is the racer to be removed, return the head's next node.
3) Traverse the linked list to find the node with the specified value.
4) If found, update the `next` pointer of the previous node to skip the node with the value.
5) Return the modified head of the linked list.
⚠️ Common Mistakes
next
pointers, leading to incorrect deletion.Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
# For testing
def print_linked_list(head):
current = head
while current:
print(current.value, end=" -> " if current.next else "\n")
current = current.next
def remove_racer(head, racer):
# Case 1: The list is empty
if head is None:
return head
# Case 2: The node to remove is the head node
if head.value == racer:
return head.next
# Case 3: The node to remove is somewhere in the middle or at the end
current = head
while current.next and current.next.value != racer:
current = current.next
# If we found the racer, remove the node
if current.next:
current.next = current.next.next
return head
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example:
head = Node("Daisy", Node("Mario", Node("Toad", Node("Mario"))))
# Linked List: Daisy -> Mario -> Toad -> Mario
print_linked_list(remove_racer(head, "Mario"))
# Expected Output: "Daisy -> Toad -> Mario"
# Linked List: Daisy -> Toad -> Mario
print_linked_list(remove_racer(head, "Yoshi"))
# Expected Output: "Daisy -> Toad -> Mario"
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
- Time Complexity: O(N) because we may need to traverse all the nodes to find the racer.
- Space Complexity: O(1) because we are only modifying the pointers and not using extra space.