/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
ListNode*p1 =pHead1;
ListNode*p2 =pHead2;
int n=0,m=0;
if(pHead1==pHead2)
{
return pHead1;
}
while(p1!=NULL)
{
p1=p1->next;
n++;
}
while(p2!=NULL)
{
p2=p2->next;
m++;
}
if(n>=m)
{
for(int i =0;i<(n-m);i++)
{
pHead1=pHead1->next;
}
}
else{
for(int i =0;i<(m-n);i++)
{
pHead2=pHead2->next;
}
}
for(int i = 0;i<min(m,n);i++)
{
if(pHead1==pHead2)
{
break;
}
pHead1 = pHead1->next;
pHead2 = pHead2->next;
}
return pHead1;
}
};