Codepath

Controlled Burns

TIP102 Unit 6 Session 2 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 20-30 mins
  • 🛠️ Topics: Linked Lists, Deletion, Traversal

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 does the problem ask for?
    • The problem asks to traverse a linked list and keep the first m nodes, then remove the next n nodes, repeating this pattern until the end of the list.
  • What should be returned?
    • The function should return the head of the updated linked list.
HAPPY CASE
Input: trailhead = Node(1, Node(2, Node(3, Node(4, Node(5, Node(6, Node(7, Node(8, Node(9, Node(10))))))))))
       m = 2
       n = 3
Output: 1 -> 2 -> 6 -> 7 -> 11 -> 12
Explanation: The function keeps the first 2 nodes, deletes the next 3 nodes, and repeats the process until the end.

EDGE CASE
Input: trailhead = Node(1, Node(2, Node(3)))
       m = 1
       n = 2
Output: 1
Explanation: The function keeps the first node, deletes the next 2 nodes, and stops as it reaches the end of the list.

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 involving Selective Deletion, we want to consider the following approaches:

  • Traversal: Traverse the linked list while keeping track of the number of nodes to be retained or deleted.
  • Pointer Manipulation: Carefully adjust pointers to skip over nodes that need to be removed.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: We will traverse the linked list, retaining the first m nodes and then deleting the next n nodes, repeating this process until the end of the list.

1) Initialize a pointer `current` to the head of the list.
2) While `current` is not None:
    a) Traverse the first `m` nodes, moving `current` forward.
    b) Check if `current` is None after traversing `m` nodes. If yes, return the head.
    c) Traverse the next `n` nodes and remove them by updating the `next` pointer of the `m-th` node.
    d) Move `current` to the node after the deleted nodes.
3) Return the head of the list.

⚠️ Common Mistakes

  • Forgetting to handle cases where the list is shorter than m + n nodes.
  • Incorrectly managing pointers, leading to loss of nodes or incorrect list structure.

4: I-mplement

Implement the code to solve the algorithm.

class Node:
    def __init__(self, value, next=None):
        self.value = value
        self.next = next

# Function to selectively clear the trail
def selective_trail_clearing(trailhead, m, n):
    current = trailhead
    
    while current:
        # Traverse m nodes
        for i in range(1, m):
            if current is None:
                return trailhead
            current = current.next

        if current is None:
            return trailhead

        # Now current is at the m-th node
        # We will delete the next n nodes
        temp = current.next
        for j in range(n):
            if temp is None:
                break
            temp = temp.next

        # Connect the m-th node to the node after the n deleted nodes
        current.next = temp

        # Move current to the next kept node
        current = temp

    return trailhead

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Example: Use the provided trailhead linked list with the values of m and n to verify that the function correctly clears the trail by removing the appropriate nodes.

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 each node is visited exactly once.
  • Space Complexity: O(1) because the algorithm uses a constant amount of extra space for pointers.
Fork me on GitHub