1.快慢指针法,啥也不是
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
int m = 0,n=0;
ListNode* temp = pHead1;
while(temp) {
m++;
temp = temp->next;
}
temp = pHead2;
while(temp) {
n++;
temp = temp->next;
}
if(m>n){
for(int i=0;i<m-n;i++) {
pHead1 = pHead1->next;
}
} else {
for(int i=0;i<n-m;i++) {
pHead2 = pHead2->next;
}
}
while(pHead1!=pHead2 && pHead1 && pHead2) {
pHead1 = pHead1->next;
pHead2 = pHead2->next;
}
return pHead1;
}
};