题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
思路:双指针交叉遍历两个链表,再次重合既为第一个公共节点
class Solution:
def FindFirstCommonNode(self , pHead1 , pHead2 ):
if not pHead1 or not pHead2:
return
p1, p2 = pHead1, pHead2
while p1 != p2:
p1 = p1.next
p2 = p2.next
if p1 == p2:
break
if not p1:
p1 = pHead2
if not p2:
p2 = pHead1
return p1