架构师系列 9: 找出单向链表合并节点
发布于: 2020 年 12 月 12 日
def check(head_a, head_b): if head_a is None or head_b is None or head_a.next is None or head_b.next is None: return None # 空间间复杂度O(n) hashset = set() hashset.clear() p = head_a.next # 时间复杂度O(n) while p.next is not None: hashset.add(p.next) p = p.next print("link_a addr: {0}".format(hashset)) p = head_b.next while p is not None: if p.next in hashset: print("link_b addr 相交节点地址: {0} 值:{1}".format(p.next, p.data)) return p.data # 相交元素 print("link_b addr: {0}".format(p.next)) p = p.next print("不相交") return None复制代码
使用 hashset 存储链表地址,判断两个链表地址是否有相同。
链表合并不是值相同而是地址相同才代表是同一个位置
划线
评论
复制
发布于: 2020 年 12 月 12 日阅读数: 20
桃花原记
关注
还未添加个人签名 2018.11.24 加入
还未添加个人简介











评论