首页 > 其他 > 详细

141. 环形链表

时间:2020-05-09 15:19:09      阅读:57      评论:0      收藏:0      [点我收藏+]

技术分享图片
技术分享图片
技术分享图片
技术分享图片

方法一思路:

快慢指针。

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

方法二思路:

用list或set。

class Solution(object):
    def hasCycle2(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        list = []
        while head:
            if head in list:
                return True
            list.append(head)
            head = head.next
        return False

141. 环形链表

原文:https://www.cnblogs.com/panweiwei/p/12857116.html

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