struct ListNode{
int data;
ListNode* next;
};
ListNode* FirstShareNode(ListNode* pHead1,ListNode* pHead2){
if(!pHead1||!pHead2) return NULL;
int len1=0,len2=0;
ListNode* pNode1=pHead1;
ListNode* pNode2=pHead2;
//get List1 length
while(pNode1){
pHead1=pHead1->next;
len1++;
}
//get List2 length
while(pNode2){
pHead2=pHead2->next;
len2++
}
//long list go first
if(len1>len2){
for(int i=0;i<len1-len2;++i){
pNode1=pNode1->next;
}
}
else{
for(int i=0;i<len2-len1;++i){
pNode2=pNode2->next;
}
}
//get the 1st share node
while(pNode1!=pNode2 && pNode1 && pNode2){
pNode1=pNode1->next;
pNode2=pNode2->next;
}
//no share node
if(!pNode1) return NULL;
return pNode1;
}