题解 | #删除链表中重复的结点#

删除链表中重复的结点

https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
#include <unordered_map>
#include <utility>
class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead) {
        if(pHead==nullptr)
        {
            return nullptr;
        }
        ListNode* cur = pHead;
        unordered_map<int, int>key;
        
        while(cur!=nullptr)
        {
            key[cur->val]++;
            cur=cur->next;
        }

        ListNode* res = new ListNode(0);
        res->next = pHead;
        cur = res;

        while(cur->next!=nullptr)
        {
            if(key[cur->next->val]!=1)
            {
                cur->next = cur->next->next;
            }
            else {
                cur = cur->next;
            }
        }
        return res->next;


        
    }
};

全部评论

相关推荐

06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务