You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
369 B
Ruby

def find_linked_list_cycle(list)
return false unless list
slow = list.head
fast = list.head
while fast && !fast.next.nil?
slow = slow.next
fast = fast.next.next
if slow == fast
break
end
end
return false if fast.nil? || fast.next.nil?
until slow == fast
slow = head
slow = slow.next
fast = fast.next
end
fast
end