题解 | #删除有序链表中重复的元素-II#

删除有序链表中重复的元素-II

https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024

import java.util.*;

public class Solution {
    public ListNode deleteDuplicates (ListNode head) {
        ListNode node=head;
        HashMap<Integer,Integer> map=new HashMap<>();
        while(head!=null){
            if(map.containsKey(head.val)){
                map.put(head.val,map.get(head.val)+1);
            }else{
                map.put(head.val,1);
            }
            head=head.next;
        }
        ListNode list=new ListNode(-1);
        list.next=node;
        ListNode cur=list;
        while(node!=null){
            if(map.get(node.val)>1){
                list.next=list.next.next;
            }else{
                list=list.next;
            }
            node=node.next;
        }

        return cur.next;
    }
}

全部评论

相关推荐

uu们,拒offer时hr很生气怎么办我哭死
爱睡觉的冰箱哥:人家回收你的offer,或者oc后没给你发offer的时候可不会愧疚你,所以你拒了也没必要愧疚他。
点赞 评论 收藏
分享
仁者伍敌:服务员还要脱颖而出,这是五星级酒店吗
点赞 评论 收藏
分享
05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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