/*

struct ListNode {

    int val;

    struct ListNode *next;

    ListNode(int x) :

            val(x), next(NULL) {

    }

};*/

class Solution {

public:

    ListNode* FindFirstCommonNodeListNode* pHead1ListNode* pHead2) {

        int len1=0;

        int len2=0;

        ListNode* p1=pHead1;

        ListNode* p2=pHead2;

        while(p1!=NULL){

            len1+=1;

            p1=p1->next;

        }

        while(p2!=NULL){

            len2+=1;

            p2=p2->next;

        }

        if(len1==0||len2==0){

            return NULL;

        }

        int count=0;

        if(len1>len2){

            while(count!=len1-len2){

                count+=1;

                pHead1=pHead1->next;

            }

            while(pHead1!=NULL){

                if(pHead1==pHead2){

                    break;

                }

                pHead1=pHead1->next;

                pHead2=pHead2->next;

            }

        }

        else{

            while(count!=len2-len1){

                count+=1;

                pHead2=pHead2->next;    

            }

            while(pHead1!=NULL){

                if(pHead1==pHead2){

                    break;

                }

                pHead1=pHead1->next;

                pHead2=pHead2->next;

            }

        }

        return pHead1;

    }

};