题解 | 判断链表中是否有环
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # # @param head ListNode类 # @return bool布尔型 # class Solution: def hasCycle(self , head: ListNode) -> bool: if head is None: return False object_set = set() cur_node = head while (cur_node.next): if cur_node in object_set: return True object_set.add(cur_node) cur_node = cur_node.next return False