这题其实源自于相遇问题,小学数学里何时超过一圈的问题大家都遇到过有木有?!!!!
class Solution:
def hasCycle(self, head: ListNode) -> bool:
#没啥说的先写特解
if head==None:
return False
slow=head
fast=head
#重要的是这个判断条件,由于走了两步,所以fast.next要判断为None
while fast!=None and fast.next!=None:
fast=fast.next.next
slow=slow.next
if fast==slow:
return True
return False