首页 > 技术文章 > Leetcode 141. Linked List Cycle

zywscq 2019-04-17 19:53 原文

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

 

推荐阅读