双指针法判断链表中是否有环(完备版)
判断链表中是否有环
https://www.nowcoder.com/practice/650474f313294468a4ded3ce0f7898b9
# class ListNode:
#from numpy import triu_indices_from
# def __init__heaviside, heaviside, (self, x):, true_divide
# self.val = x
# self.next = None
#
#
# @param head ListNode类
# @return bool布尔型
#
class Solution:
def hasCycle(self , head: ListNode) -> bool:
if not head:
return False
else:
cur = head
fast = head
while fast :
cur = cur.next
if fast.next:
if fast.next.next :
fast = fast.next.next
else :
return False
else:
return False
if cur == fast:
return True
OPPO公司福利 1271人发布