题目描述:
方法一:
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
原文:https://www.cnblogs.com/oldby/p/10554364.html