zoukankan      html  css  js  c++  java
  • 82. Remove Duplicates from Sorted List II

    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            Map<Integer,Integer> map1=new HashMap<Integer,Integer>();
            if(head==null)
                return null;
            ListNode temp=head;
            while(temp!=null)
            {
                if(!map1.containsKey(temp.val))
                    map1.put(temp.val,0);
                else
                    map1.put(temp.val, 1);
                temp=temp.next;
            }
            ListNode newHead=null;
            temp=head;
            ListNode tail=null;
            
            while(temp!=null)
            {
                if(map1.get(temp.val)==0)
                {
                    if(newHead==null)
                        newHead=temp;
                    if(tail!=null)
                        tail.next=temp;
                    tail=temp;
                    temp=temp.next;
                    
                }
                else
                {
                    temp=temp.next;
                }
            }
            if(tail!=null)
                tail.next=null;
            return newHead;
            
            
        }
    }
  • 相关阅读:
    uva-11361
    HDU
    LCS
    CodeForces
    linux 有趣的命令组合
    opencv识别封闭区域 并标记该区域
    宜出行人口热力图
    美团酒店
    赶集租房
    发送企业微信应用通知
  • 原文地址:https://www.cnblogs.com/aguai1992/p/5678384.html
Copyright © 2011-2022 走看看