Codepath

Saharah

TIP102 Unit 5 Session 1 (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 15 mins
  • 🛠️ Topics: Linked List

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 to the tom_nook node after it is removed?
    • It is disconnected from the list and its next pointer is set to None.
  • Where should the new node saharah be added?
    • after tommy

Initial list: tom_nook -> timmy -> tommy

After disconnecting tom_nook: timmy -> tommy

After adding saharah: timmy -> tommy -> saharah

HAPPY CASE
Input: A linked list with nodes `tom_nook -> timmy -> tommy`
Output: A linked list with nodes `timmy -> tommy -> saharah`
Explanation: The `tom_nook` node is removed and the `saharah` node is added to the end.

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:

  • Traversing the list
  • Modifying pointers

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: We need to disconnect the tom_nook node from the list and then add a new node saharah to the end of the list.

1) Create the initial linked list with nodes `tom_nook -> timmy -> tommy`.
2) Disconnect the `tom_nook` node by setting `timmy.next` to `tommy`.
3) Set `tom_nook.next` to `None`.
4) Create a new node `saharah`.
5) Add `saharah` to the end of the list by setting `tommy.next` to `saharah`.

⚠️ Common Mistakes

  • Forgetting to properly disconnect the tom_nook node.
  • Incorrectly setting the next pointers, causing a loop or disconnection in the list.

4: I-mplement

Implement the code to solve the algorithm.

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

# From previous problem
tom_nook = Node("Tom Nook")
tommy = Node("Tommy")
timmy = Node("Timmy")
tom_nook.next = timmy
timmy.next = Tommy


# Disconnect tom_nook from the rest of the list
tom_nook.next = None

# Create Saharah
saharah = Node("Saharah")
tommy.next = saharah

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

Initial list: tom_nook -> timmy -> tommy

After disconnecting tom_nook: timmy -> tommy

After adding saharah: timmy -> tommy -> saharah

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 need to traverse the list to find the nodes to modify.
Space Complexity: O(1) because we are only using a fixed amount of additional space.
Fork me on GitHub