class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        if head == None:
            return False
        try:
            p1 =head 
            p2 = head.next
        except:
            return False
        while p1:
            if p1 == p2:
                return True
            else:
                try:
                    p1 =p1.next
                    p2 = p2.next
                    p2 = p2 .next
                except:
                    return False
        return False
Copy the code