/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
int l1 = 0; // 记录两个链表的长度,并让链表较长的先走,令两个链表之后的长度相同
int l2 = 0;
auto h1 = pHead1;
auto h2 = pHead2;
while(h1) {
h1 = h1->next;
l1++;
}
while(h2) {
h2 = h2->next;
l2++;
}
int n = l1 - l2;
if(n > 0)
while(n--)
pHead1 = pHead1->next;
else {
n = abs(n);
while(n--)
pHead2 = pHead2->next;
}
while(pHead1 != pHead2) {
pHead1 = pHead1->next;
pHead2 = pHead2->next;
}
return pHead1;
}
};