【leetcode】141. 环形链表(python)

【leetcode】141. 环形链表(python),第1张


思路:哈希表
# 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
        """
        nums = set()
        while head:
            if head in nums:
                return True
            else:
                nums.add(head)   # note: not add head.val
                head = head.next
        return False

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/732450.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存