/** * 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; } }