首页 > 其他 > 详细

leetcood学习笔记-141-环形列表

时间:2019-03-18 20:11:45      阅读:151      评论:0      收藏:0      [点我收藏+]

题目描述:

技术分享图片

技术分享图片

方法一:

class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        while head:
            if head.val == "daafas":
                return True
            else:
                head.val = "daafas":
            head = head.next
        return False

 

法二:判断某个元素是否在集合中只有O(1),

class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        save = set()
        cur = head 
        while cur is not None:
            if cur in save:
                return True
            else:
                save.add(cur)
                cur = cur.next
        return False 

技术分享图片

 

leetcood学习笔记-141-环形列表

原文:https://www.cnblogs.com/oldby/p/10554364.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!