My accepted Java solution


  • 0
    S
     public ListNode deleteDuplicates(ListNode head) {
        //dummy Head to handle corner cases efficiently
        ListNode dummyHead = new ListNode(Integer.MIN_VALUE); 
        ListNode curr = head;
        ListNode temp = dummyHead; //temp is the tail of the list without duplicates.
            while(curr!=null )
            {
                int count =1;
                while(curr !=null &&  curr.next != null && curr.val == curr.next.val)
                {
                    curr = curr.next;
                    count++;
                }
     /*if count is one ,the element occured only once.We add this node to non-duplicate list tail */
                if(count ==1)
                {
                    temp.next = curr;
                    temp = curr;
                }
                curr = curr.next;            
               
            }
            temp.next = null;
           return dummyHead.next; 
        }
    

Log in to reply
 

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