链表相交的第一个节点

 1 # -*- coding:utf-8 -*-
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6 """首先判断长度,然后移动指针将两个链表尾部对齐,当两链表的头指针相等时,即为交点"""
 7 class Solution:
 8     def FindFirstCommonNode(self, pHead1, pHead2):
 9         # write code here
10         if pHead1==None or pHead2==None:
11             return None
12         la=self.len_list(pHead1)
13         lb=self.len_list(pHead2)
14         if la>lb:
15             i=0
16             while i<la-lb:
17                 pHead1= pHead1.next
18                 i+=1
19         if la<lb:
20             i=0
21             while i<la-lb:
22                 pHead2= pHead2.next
23                 i+=1
24         while pHead1!=pHead2:
25             pHead1= pHead1.next
26             pHead2= pHead2.next
27         return pHead1
28     def len_list(self,Head):  #定义一个新的方法也是要先判断极端情况
29         if Head==None:
30             return None
31         count=0
32         while Head:  #head肯定不是none,已经判断了上面,
33             Head=Head.next
34             count+=1
35         return count
36  
原文地址:https://www.cnblogs.com/Henry-ZHAO/p/12725301.html