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

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

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

😔😔😔

import java.util.*;

public class Solution {
    public ListNode deleteDuplicates (ListNode head) {
        if(head==null||head.next==null)
            return head;

        ListNode newHead = new ListNode(0);
        ListNode pre = newHead;

        HashMap<Integer,Integer> hm = new LinkedHashMap<>();

        while(head!=null)
        {
            if(hm.containsKey(head.val))
                hm.put(head.val,hm.get(head.val)+1);
            else
                hm.put(head.val,1);
            head = head.next;
        }

        for(Integer keys : hm.keySet()){
            if(hm.get(keys)==1){
                pre.next = new ListNode(keys);
                pre = pre.next;
            }
        }

        pre = null;
        return newHead.next;
    }
}
全部评论

相关推荐

想按时下班的我在等o...:我投测试也是这个情况,不知道咋办了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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