programming-examples/ruby/Data_Structures/find_linked_list_cycle.rb

24 lines
369 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
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