<span style="font-family: Arial, Helvetica, sans-serif;">/*</span>
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2) {
if(pHead1 == NULL || pHead2 == NULL)
return NULL;
int len1;
int len2;
len1 = getlen(pHead1);
len2 = getlen(pHead2);
if(len1 > len2)
{
for(; len1 > len2; len1--)
{
pHead1 = pHead1 -> next;
}
}
else
{
for(; len2 > len1; len2--)
{
pHead2 = pHead2 -> next;
}
}
while(pHead1 != pHead2)
{
pHead1 = pHead1 -> next;
pHead2 = pHead2 -> next;
}
return pHead1;
}
int getlen(ListNode * p)
{
int l = 0;
while(p != NULL)
{
l++;
p = p -> next;
}
return l;
}
};