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

 

转载于:https://www.cnblogs.com/zywscq/p/10725750.html

你可能感兴趣的:(Leetcode 141. Linked List Cycle)