由于知道链表最大长度,于是可以耍赖,当循序次数超过最大长度,意味着有环,否则无环。

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 23 11:21:11 2022

@author: Administrator
"""
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

#
# 
# @param head ListNode类 
# @return bool布尔型
#
class Solution:
    def hasCycle(self , head: ListNode) -> bool:
        tmp = head
        index = 0
        while tmp and index < 10002:
            index+=1
            tmp=tmp.next
        if index>10001:
            return True
        else:
            return False