Codepath

Remove From Inventory

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.
  • What happens if the item is not found in the list?
    • If the item is not found, the function should return the list unchanged.
  • What happens if the item to be deleted is the first node?
    • The head should be updated to the next node.
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.

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 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

  • Forgetting to handle the case where the linked list is empty.
  • Not correctly updating the next pointers, leading to incorrect deletions.

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 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

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
  • Catch possible edge cases and off-by-one errors

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

6: E-valuate

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.

  • Time Complexity: O(N) because we may need to traverse all the nodes to find the item.
  • Space Complexity: O(1) because we are only modifying the pointers and not using extra space.
Fork me on GitHub