TIP102 Unit 6 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?
HAPPY CASE
Input: marker1 = Node("Trailhead")
marker2 = Node("Trail Fork")
marker3 = Node("The Falls")
marker4 = Node("Peak")
marker1.next = marker2
marker2.next = marker3
marker3.next = marker4
marker4.next = marker2 # Cycle here
Output: Trailhead -> Trail Fork -> The Falls -> Peak
Explanation: The cycle is removed, resulting in a straightforward linked list.
EDGE CASE
Input: marker1 = Node("Trailhead")
marker1.next = marker1 # Self-loop
Output: Trailhead
Explanation: The self-loop is removed, leaving a single node.
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 Cycle Detection and Removal, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We will first use Floyd's Cycle Detection Algorithm to detect the cycle in the linked list. If a cycle is detected, we will find the start of the cycle and remove it by making the last node in the cycle point to None
.
1) Use Floyd's Cycle Detection Algorithm:
a) Initialize two pointers, `slow` and `fast`, both pointing to the head of the list.
b) Move `slow` by one step and `fast` by two steps.
c) If `slow` meets `fast`, a cycle is detected.
2) If a cycle is detected, find the starting node of the cycle:
a) Reset `slow` to the head of the list.
b) Move both `slow` and `fast` one step at a time until they meet. The meeting point is the start of the cycle.
3) Traverse the cycle to find the node that points back to the start of the cycle and set its next pointer to `None`.
4) Return the head of the list.
⚠️ Common Mistakes
next
pointer set to None
.Implement the code to solve the algorithm.
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
# Function to remove a cycle in the linked list
def clear_trail(trailhead):
if not trailhead:
return None
slow = trailhead
fast = trailhead
# Step 1: Detect cycle using Floyd's Cycle Detection Algorithm
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
break
else:
# No cycle detected
return trailhead
# Step 2: Find the start of the cycle
slow = trailhead
while slow != fast:
slow = slow.next
fast = fast.next
# Step 3: Remove the cycle
while fast.next != slow:
fast = fast.next
fast.next = None
return trailhead
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
marker1
, marker2
, marker3
, and marker4
linked list with a cycle to verify that the function correctly removes the cycle.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.
O(N)
because each node is visited exactly once during cycle detection and removal.O(1)
because the algorithm uses a constant amount of extra space for pointers.