20201021-141环形链表

it2024-04-10  49

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 """ slow = fast = head if head: while fast and fast.next: fast = fast.next.next slow = slow.next if fast == slow: return True return False 快慢指针一个快一个慢如果相遇,说明有圈圈
最新回复(0)