判断链表是否有环
方法一:set集合法,数据节点存在则说明有环
def create_circle_linklist(): a = Node(1) b = Node(2) c = Node(3) d = Node(4) a.next = b b.next = c c.next = d d.next = b return a def check_circle(head): a = set() while head: if head not in a: a.add(head) else: return False head = head.next return True if __name__ == '__main__': print(check_circle(create_circle_linklist()))
时刻记着自己要成为什么样的人!