题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # # @param pHead1 ListNode类 # @param pHead2 ListNode类 # @return ListNode类 # class Solution: def FindFirstCommonNode(self , pHead1 , pHead2 ): if pHead1 is None or pHead2 is None: #有一链为空则共享链为空,返回None return None inner = pHead2 #内链首部 outer = pHead1 #外链首部 while outer:#遍历外链 while inner: #遍历内链 if inner == outer: #内外连节点相等,即找了共享链表 return inner inner = inner.next inner = pHead2 outer = outer.next else: #没有找到同用部分就返回None return None return False