双指针法,将两条链表按不同顺序相连,即tmp1等于pHead1的末尾接pHead2,tmp2等于pHead2的末尾接pHead1。
同时对tmp1和tmp2遍历,存在一处使得两个指针相遇,即为第一个公共节点。

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        ListNode *tmp1 = pHead1, *tmp2 = pHead2;
        while (tmp1 != tmp2) {
            tmp1 = (tmp1 == nullptr ? pHead2 : tmp1->next);      // pHead1的末尾接pHead2
            tmp2 = (tmp2 == nullptr ? pHead1 : tmp2->next);      // pHead2的末尾接pHead1
        }
        return tmp1;
    }
};