判断链表中是否有环 -- 每日一题04
'''
定义俩个步长不一样的指针,一个每次走一步,一个每次走俩步
如果快慢指针相遇则表示链表中有环,如果没有相遇快指针就走到了链表末尾,那么表示链表没有环
'''
class Solution:
def hasCycle(self , head: ListNode) -> bool:
slow = head
fast = head
while fast is not None and fast.next is not None:
slow = slow.next
fast = fast.next.next
if fast == slow:
break
if fast is None or fast.next is None:
return False
return True
