# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def FindFirstCommonNode(self, pHead1, pHead2):
# write code here
cur1=pHead1
cur2=pHead2
j=0
while True:
if cur1:
j=j+1
cur1=cur1.next
else:
break
while True:
if cur2:
j=j-1
cur2=cur2.next
else:
break
cur1=pHead1
cur2=pHead2
if j>0: #此时j为两个链表长度差
for i in range(j):
cur1=cur1.next #使两个链表长度相等,移动j次后使cur1与cur2在相同位置
elif j<0:
j=abs(j)
for i in range(j):
cur2=cur2.next
else: #j=0
cur1=pHead1
cur2=pHead2
while True:
if cur1 == cur2:
return cur1
else:
cur1=cur1.next
cur2=cur2.next