找两个链表的共同点

来源:互联网 发布:哈希map java 编辑:程序博客网 时间:2024/06/10 16:12
pNode* findCommonList(pNode* l1, pNode* l2) {    if (!l1 || !l2)        return NULL;    pNode dummy;    pNode* tail = &dummy;    map<int, int> hashTab; // record the node exits    pNode* l = l1;    while(l) {        if (!hashTab.find(l->data)            map.insert(l->data, 1);          l++;    }    while(l2) {        if (hashTab.find(l2->data)) {            pNode* node = new pNode(l2->data);              tail->next = node;            tail = node;        }        l2++;    }    return dummy.next;}
0 0