java :: remove duplicates ::

  • 0
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
    class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            HashMap<Integer,Integer> c_map=new HashMap(); //maintains count
     		ListNode t=head;
     		while (t!=null) {
     		return compute(head,c_map);
         ListNode compute(ListNode head,HashMap<Integer,Integer> map){
         if(head==null) return null;
         if (map.get(head.val)==1) { //spare this node,compute for next
         head=compute(,map); //remove this node and check for next
         return head;
    1. storing count of a node value in hash map.
    2. compute either spares it if occurrence is only once or replaces with the reference of result.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.