题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
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 ):
# write code here
l=[]
cur1=pHead1
cur2=pHead2
if cur1==None or cur2==None:
return None
while pHead1:
l.append(cur1)
cur1=pHead1.next
pHead1=pHead1.next
while pHead2:
cur2=pHead2#链表2 一定要从第一位开始
if cur2 in l:
return cur2
else:
pHead2=pHead2.next
return None