链表2个的公共节点

关键点:可以用HashMap来实现

/*
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;
        }       
 
        map<ListNode *,int> m_map;
        
        while(pHead1!=NULL)
            {
            m_map[pHead1]=1;
            pHead1=pHead1->next;
        }
        
        while(pHead2!=NULL)
            {
            if(m_map[pHead2])
                return pHead2;
            pHead2=pHead2->next;
        }
        return NULL;
    }
};

猜你喜欢

转载自blog.csdn.net/u010325193/article/details/86322600