# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def FindFirstCommonNode(self, pHead1, pHead2):
m = self.getLen(pHead1)
n = self.getLen(pHead2)
if m < n:
pHead1, pHead2 = pHead2, pHead1
m, n = n, m
for i in range(m-n):
pHead1 = pHead1.next
while pHead1 and pHead2:
if pHead1 == pHead2:
return pHead1
pHead1 = pHead1.next
pHead2 = pHead2.next
def getLen(self, p):
m = 0
while p:
m += 1
p = p.next
return m
网友评论