# 题目描述
# 输入两个链表，找出它们的第一个公共结点。

# -*- 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
        if pHead1 == None: return None
        if pHead2 == None: return None
        
        p1 = pHead1
        p2 = pHead2
        while p1 != None and p2 != None:
            p1 = p1.next
            p2 = p2.next
            
        if p1 == None:
            p1 = pHead2
        if p2 == None:
            p2 = pHead1
            
        while p1 != None and p2 != None:
            p1 = p1.next
            p2 = p2.next
            
        if p1 == None:
            p1 = pHead2
        if p2 == None:
            p2 = pHead1
            
        while p1 != p2:
            p1 = p1.next
            p2 = p2.next
        
        return p1