# @param {ListNode} head # @return {ListNode} def detectCycle(head) begin t=head.next h=head.next.next while t!=h t=t.next h=h.next.next end t=head while t!=h t=t.next h=h.next end t rescue NoMethodError nil end end
Floyd's Cycle Detection Algorithm