Codepath

Saharah

TIP102 Unit 5 Session 1 Standard (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.

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

Initial list: tom_nook -> timmy -> tommy After disconnecting tom_nook: timmy -> tommy After adding saharah: timmy -> tommy -> saharah

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. should be added to the end of the list after tommy.

HAPPY CASE
Input: A linked list with nodes `timmy -> tom_nook -> 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.

EDGE CASE
Input: A linked list with a single node `tom_nook`
Output: A linked list with nodes `saharah`
Explanation: The `tom_nook` node is removed and the `saharah` node is the only node left.

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 `timmy -> tom_nook -> 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")
tom_nook.next = tommy

timmy = Node("Timmy", tom_nook)

# Disconnect tom_nook from the rest of the list
timmy.next = tommy
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: timmy -> tom_nook -> 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