141. 环形链表
方法一
# 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
原文地址:https://www.cnblogs.com/xiao-xue-di/p/10286623.html
时间: 2024-11-09 05:38:55