leetcood學習筆記-141-環形列表
阿新 • • 發佈:2019-03-18
-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-環形列表