标签:second fir pre def one 方法 ext bsp lin
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def hasCycle(self, head): """ :type head: ListNode :rtype: bool """ if not head: return False first = head # 快指针 second = head # 慢指针 # 如果列表不存在环,最终快指针先到达尾部,False # 如果列表中存在环,快指针最终一定会追上慢指针 while 1: if first.next is None or first.next.next is None: return False first = first.next.next second = second.next if first == second: return True
标签:second fir pre def one 方法 ext bsp lin
原文地址:https://www.cnblogs.com/xiao-xue-di/p/10286623.html