Leetcode 141. Linked List Cycle

floyd circle detection algorithm(龟兔赛跑算法)

class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        try:
            slow = head
            fast = head.next
            while slow is not fast:
                slow = slow.next
                fast = fast.next.next
            return True
        except:
            return False

 

posted @ 2019-04-17 19:53  周洋  阅读(205)  评论(0编辑  收藏  举报