#题目内容
给定一个链表,判断链表中是否有环。
为了表示给定链表中的环,我们使用整数pos来表示链表尾连接到链表中的位置(索引从0开始)。如果pos是-1,则在该链表中没有环。
class Solution:
def hasCycle(self, head: ListNode) -> bool:
if head==None:
return False
target = {head}
head=head.next
while head:
if head in target:
return True
else:
target.add(head)
head=head.next
return False