1. 程式人生 > >leetcood學習筆記-141-環形列表

leetcood學習筆記-141-環形列表

-c bubuko 題目 etc div lis () head png

題目描述:

技術分享圖片

技術分享圖片

方法一:

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-環形列表