class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None or head.next is None:
return None
slow = head
fast = head
while fast is not None and fast.next is not None: # 注意这里的条件,先要确保有fast,才能确保有fast next
slow = slow.next # 曾经在这里判断了二者不等才移动,但其实起点时二者也相等
fast = fast.next.next
if slow == fast:
fast = head
while slow != fast:
slow = slow.next
fast = fast.next
return slow
return None