Commit 5cbfc45

mo <mokha@cisco.com>
2017-07-06 02:32:07
calculate length of cycle.
1 parent f6b36c3
Changed files (1)
spec/cycle_spec.rb
@@ -52,6 +52,24 @@ describe "cycle" do
     0
   end
 
+  def cycle_length(head)
+    return 0 if head.nil?
+
+    tortoise, hare = head, head
+    d1, d2 = 0, 0
+
+    while hare && tortoise
+      break if tortoise == hare && hare != head
+
+      tortoise, hare = tortoise.next_item, hare.next_item&.next_item
+      d1 += 1
+      d2 += 2
+    end
+    return 0 if hare.nil?
+
+    d2 - d1
+  end
+
   def entry_point(head)
     return nil if head.nil?