# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
nums = set()
while head:
if head in nums:
return True
else:
nums.add(head) # note: not add head.val
head = head.next
return False
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)