其实就是相当于把两个链表给串接起来,然后找出相似的值。
图片说明
解法一:

public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        if(pHead1 == null || pHead2 ==null){
            return null;
        }
        //先求出各自的长度
        ListNode temp1 = pHead1;
        ListNode temp2 = pHead2;
        int Temp1Count = 0;
        int Temp2Count = 0;
        while (true){
            if(temp1.next == null){
                break;
            }
            temp1 = temp1.next;
            Temp1Count++;
        }

        while (true){
            if(temp2.next == null){
                break;
            }
            temp2 = temp2.next;
            Temp2Count++;
        }
        //如果最后一个不相等,就直接返回null
        if(temp1.val != temp2.val){
            return null;
        }
        //确定出长的链表和短的链表
        ListNode minTemp = null;
        ListNode maxTemp = null;
        int minCount = 0;
        int maxCount = 0;
        if(Temp1Count < Temp2Count){
            minTemp = pHead1;
            maxTemp = pHead2;
            minCount = Temp1Count;
            maxCount = Temp2Count;
        }else{
            minTemp = pHead2;
            maxTemp = pHead1;
            minCount = Temp2Count;
            maxCount = Temp1Count;
        }
        //再让长的前进道短的长度的位置,然后依次比较
        for (int i = 0; i < maxCount - minCount; i++) {
            maxTemp = maxTemp.next;
        }
        while(maxTemp.val != minTemp.val){
            maxTemp = maxTemp.next;
            minTemp = minTemp.next;
        }
        return maxTemp;
    }
}

图片说明
解法二:

public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        if(pHead1 == null || pHead2 == null){
            return null;
        }
        ListNode temp1 = pHead1;
        ListNode temp2 = pHead2;
        while(temp1 != temp2){
            temp1 = temp1.next;
            temp2 = temp2.next;
            if (temp1 != temp2){
                if(temp1 == null){
                    temp1 = pHead2;
                }
                if(temp2 == null){
                    temp2 = pHead1;
                }
            }
        }
        return temp1;
    }
}

图片说明