83. Remove Duplicates from Sorted List

/**
 * 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> mp=new HashMap<Integer,Integer>();
        if(head==null)
            return null;
        ListNode temp=head;
        ListNode tail=null;
        while(temp!=null)
        {
            if(mp.containsKey(temp.val))
            {
                if(tail!=null)
                    tail.next=temp.next;
                else
                    head=temp.next;
            }
            else
            {
                mp.put(temp.val,1);
                tail=temp;
            }
            temp=temp.next;
        }
        return head;
    }
}
原文地址:https://www.cnblogs.com/aguai1992/p/5351461.html