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

删除链表中重复的结点

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

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead) {
        if(pHead == NULL){
            return NULL;
        }
        if(pHead->next == NULL){
            return pHead;
        }
        unordered_map <int,ListNode*> count_table;
        ListNode* front = pHead;
        ListNode* temp;
       
     
        while(front !=NULL){
            auto it = count_table.find(front->val);
            if ( it == count_table.end()){
                count_table.emplace(front->val,front);
              
            }
            else{
                it->second = NULL;
            }
            front = front->next;
         

        }
        front=pHead;
        pHead = NULL;
        while(front !=NULL){
            auto it = count_table.find(front->val);
            front = front->next;
          
            if( it->second == NULL){
                continue;
            }
            else{
                if(pHead == NULL){
                    pHead = it->second;
                    pHead->next = NULL;
                    temp = pHead;
                    cout<<temp->val<<endl;
                    continue;
                }
                temp->next = it->second;
                temp = temp->next;
                cout<< temp->val<<endl;
                temp->next =NULL;
            }
        }       
        return pHead;

    }
};

全部评论

相关推荐

秋盈丶:后续:我在宿舍群里和大学同学分享了这事儿,我好兄弟气不过把他挂到某脉上了,10w+阅读量几百条评论,直接干成精品贴子,爽
点赞 评论 收藏
分享
风中翠竹:真的真的真的没有kpi。。。面试官是没有任何kpi的,捞是真的想试试看这个行不行,碰碰运气,或者是面试官比较闲现在,没事捞个人看看。kpi算HR那边,但是只有你入职了,kpi才作数,面试是没有的。
点赞 评论 收藏
分享
昨天 18:30
门头沟学院 Java
据说名字越长别人越关注你的昵称我觉得我要被关注了:你问问这里面有多少是正经候选人,而不是乱打招呼的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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