# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if head==None:
return False
slow,fast=head,head.next
if fast==None:
return False
while slow!=fast:
if fast.next==None:
return False
elif fast.next.next==None:
return False
slow=slow.next
fast=fast.next.next
return True
还是双指针方法灵活
来源:https://blog.csdn.net/weixin_45569078/article/details/101036155