/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of the linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
if(head==null){
return null;
}
LinkedHashMap<Integer,Integer> map=new LinkedHashMap<Integer,Integer>();
map.put(head.val, 1);
while(head.next!=null){
head=head.next;
if(map.containsKey(head.val)){
map.put(head.val, map.get(head.val)+1);
}else{
map.put(head.val, 1);
}
}
Set<Integer> ts= map.keySet();
Iterator<Integer> it=ts.iterator();
ArrayList<Integer> list=new ArrayList<Integer>();
while(it.hasNext()){
int temp=it.next();
if(map.get(temp)==1){
list.add(temp);
}
}
if(list.size()==0){
return null;
}
ListNode newHead=new ListNode(list.get(0));
ListNode node=newHead;
for(int i=1;i<list.size();i++){
node.next=new ListNode(list.get(i));
node=node.next;
}
return newHead;
}
}