快慢指针–龟兔赛跑
执行用时:64 ms, 在所有 Python3 提交中击败了66.76%的用户
内存消耗:18.1 MB, 在所有 Python3 提交中击败了54.61%的用户
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: ListNode) -> bool:
if head==None or head.next==None or head.next.next==None:
return False
p=head
q=head
while p and q and q.next:
p=p.next
q=q.next.next
if p==q:
return True
return False