解题思路:
- 设置两个指针,一个fast一个slow
- 当slow或者fast出现None时,说明这个链表不存在环
- fast在走的过程中不断的寻找slow,直到找到为止
# def __init__(self, x):
# self.val = x
# self.next = None
#
#
# @param head ListNode类
# @return bool布尔型
#
class Solution:
def hasCycle(self , head: ListNode) -> bool:
slow = head
fast = head
while slow and fast:
if fast.next:
fast = fast.next.next
else:
return False
if fast == slow:
return True
slow = slow.next
return False