/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
int len1=0,len2=0;
ListNode* p=pHead1;
ListNode* q=pHead2;
while(p){
len1++;
p=p->next;
}
while(q){
len2++;
q=q->next;
}
p=pHead1;
q=pHead2;
if(len1>len2){
int a=len1-len2;
while(a--)
p=p->next;
}
else if(len1<len2){
int b=len2-len1;
while(b--)
q=q->next;
}
while(p){
if(p==q) return p;
p=p->next;
q=q->next;
}
return NULL;
}
};
看到了很简便的方法,但是吧,我觉得常规的适合我,除了这个也可以遍历两列表放入栈中,从后向前比较相同元素。