# 求两个链表的第一个公共结点

解法2.分析两个链表，两个链从第一个公共结点开始到链尾的结点是完全重合的，（链结构性质已经决定了该属性），所以从链尾开始比较，比到第一个不相同的结点就找到了第一个公共点。时间复杂度O(m+n),空间复杂度O(m+n),(存放链表的两个栈总空间m+n，第一次存放栈的时间max(m,n),第二次比较栈内元素的时间min(m,n),所以总时间复杂度为O(m+n)).

解法3：计算出两个链表的长度，然后长的先走几步达到和短的一样长度，然后再一起走，那么时间复杂度为O(m+n),（遍历两个链表长度的时间为m+n,比较链表时间为max(m,n),所以总时间为m+n+max(m,n),这是线性的增长，所以最后的复杂度为O(m,n））所以这种最好。

class LinkNode(object):
def __init__(self,key = None,next = None):
self.key = key
self.next = next
def getLen(Link):
if Link.key is None:
return None
count = 1
while Link.next:
Link = Link.next
count += 1
return count
def getFirstCommonNode(Link1,Link2):
if Link1.key is None or Link2.key is None:
return None
len_1 = getLen(Link1)
len_2 = getLen(Link2)
diff = len_1 - len_2
long_link = Link1
short_link = Link2
if len_1 < len_2:
diff = len_2 - len_1
long_link = len_2
short_link = len_1
for i in range(diff):
long_link = long_link.next
while long_link.key is not None and short_link.key is not None and long_link.key != short_link.key:
long_link = long_link.next
short_link = short_link.next
return long_link.key

A = LinkNode()
B = LinkNode()
C = LinkNode()
D = LinkNode()
E = LinkNode()
F = LinkNode()
G = LinkNode()

A.key = 1
A.next = B
B.key = 2
B.next = C
C.key = 3
C.next = F
D.key = 4
D.next = E
E.key = 5
E.next = F
F.key = 6
F.next =G
G.key =7

print getFirstCommonNode(A,D)