Remove Racer - codepath/compsci_guides GitHub Wiki
TIP102 Unit 5 Session 2 Standard (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10-15 mins
- 🛠️ Topics: Linked Lists, Deletion
1: U-nderstand
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?
- What happens if the linked list is empty?
- If the linked list is empty, the function should return
None
.
- If the linked list is empty, the function should return
- What happens if the racer is not found in the list?
- If the racer is not found, the function should return the original list unchanged.
- What happens if the racer to be removed is the first node in the list?
- The head should be updated to the next node in the list.
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`.
2: M-atch
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:
- Traversal of a linked list
- Deletion of nodes in a linked list
3: P-lan
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
- Forgetting to handle the case where the linked list is empty.
- Not correctly updating the
next
pointers, leading to incorrect deletion.
4: I-mplement
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
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Trace through your code with an input to check for the expected output and edge cases.
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"
6: E-valuate
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.