题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
http://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
a = pHead1
b = pHead2
# 当两者相同则是第一个公共节点
while a!=b:
# a从pHead1遍历完再遍历pHead2
a = a.next if a else pHead2
# b从pHead2遍历完再遍历pHead1
b = b.next if b else pHead1
return a