struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
struct ListNode* tailA = headA;
struct ListNode* tailB = headB;
int a=0,b=0;
if( (tailA==NULL)||(tailB==NULL))
{
return NULL;
}
while(tailA){
tailA=tailA->next;
a++;
}
while(tailB){
tailB=tailB->next;
b++;
}
if(tailB != tailA){
return NULL;
}
tailA = headA;
tailB = headB;
int c=0;
if (a<b){
c=b-a;
for(int i=0;i<c;i++){
tailB=tailB->next;
}
}
else{
c=a-b;
for(int i=0;i<c;i++){
tailA=tailA->next;
}
}
while(tailA!=tailB)
{
if(tailA)
tailA=tailA->next;
if(tailB)
tailB=tailB->next;
}
return tailA;
}
|