Interview Bootcamp
Ask Author
Back to course home

0% completed

Vote For New Content
Tu Huy Nguyen
Solution#1 does not handle case when there is no cycle.

Tu Huy Nguyen

Jan 16, 2025

Should add the check if no cycle is found:

def detectCycle(self, head): cycle_length = 0 slow, fast = head while fast and fast.next: fast = fast.next.next slow = slow.next if slow == fast: cycle_length = self.calculate_cycle_length(slow) break # HERE: should handle case where no cycle is found. if fast is None or fast.next is None: return None return self.find_start(head, cycle_length)

0

0

Comments
Comments
Design Gurus
Design Gurus8 months ago

According to the problem statement, the given LinkedList contains a cycle:

Given the head of a Singly LinkedList that contains a cycle...