Unit 5 Session 2 (Click for link to problem statements)
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?
next
pointers of the appropriate nodes to insert the new nodes in the correct positions.HAPPY CASE
Input: head = Node("Shy Guy") -> Node("Diddy Kong") -> Node("Dry Bones")
Output: Node("Shy Guy") -> Node("Link") -> Node("Diddy Kong") -> Node("Toad") -> Node("Dry Bones")
Explanation: The nodes "Link" and "Toad" are correctly inserted in the list.
EDGE CASE
Input: head = Node("Shy Guy")
Output: Node("Shy Guy")
Explanation: The list contains only one node, and no new nodes are added.
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: Insert new nodes into the existing linked list by updating the next
pointers of the relevant nodes.
1) Create the new nodes "Link" and "Toad".
2) Update the `next` pointer of the node "Shy Guy" to point to "Link", and update the `next` pointer of "Link" to point to "Diddy Kong".
3) Update the `next` pointer of the node "Diddy Kong" to point to "Toad", and update the `next` pointer of "Toad" to point to "Dry Bones".
4) Print the linked list to verify the order.
⚠️ Common Mistakes
next
pointers correctly, leading to incorrect list sequences.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
shy_guy = Node("Shy Guy")
diddy_kong = Node("Diddy Kong")
dry_bones = Node("Dry Bones")
shy_guy.next = diddy_kong
diddy_kong.next = dry_bones
# Add code to update the list here
link = Node("Link")
toad = Node("Toad")
# Insert Link after Shy Guy
link.next = shy_guy.next
shy_guy.next = link
# Insert Toad after Diddy Kong
toad.next = diddy_kong.next
diddy_kong.next = toad
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example:
print("Current List:")
print_linked_list(shy_guy)
# Expected Output: "Shy Guy -> Link -> Diddy Kong -> Toad -> Dry Bones"
Evaluate the performance of your algorithm and state any strong/weak or future potential work.