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("Slingshot") -> Node("Peaches") -> Node("Scarab Beetle"), item = "Peaches"
Output: Node("Slingshot") -> Node("Scarab Beetle")
Explanation: The node with value "Peaches" is removed from the linked list.
EDGE CASE
Input: head = Node("Slingshot") -> Node("Peaches") -> Node("Scarab Beetle"), item = "Triceratops Torso"
Output: Node("Slingshot") -> Node("Peaches") -> Node("Scarab Beetle")
Explanation: The item is not found, so the list remains unchanged.
EDGE CASE
Input: head = None, item = "Peaches"
Output: None
Explanation: The linked list is empty, so 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 item to delete, 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 deletions.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 delete_item(head, item):
# Case 1: The list is empty
if head is None:
return head
# Case 2: The node to delete is the head node
if head.value == item:
return head.next
# Case 3: The node to delete is somewhere in the middle or at the end
current = head
while current.next and current.next.value != item:
current = current.next
# If we found the item, 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:
slingshot = Node("Slingshot")
peaches = Node("Peaches")
beetle = Node("Scarab Beetle")
slingshot.next = peaches
peaches.next = beetle
# Linked List: slingshot -> peaches -> beetle
print_linked_list(delete_item(slingshot, "Peaches"))
# Expected Output: slingshot -> beetle
# Linked List: slingshot -> beetle
print_linked_list(delete_item(slingshot, "Triceratops Torso"))
# Expected Output: slingshot -> beetle
Evaluate the performance of your algorithm and state any strong/weak or future potential work. Assume N represents the number of nodes in the linked list.