Iterative solution without dummy head


  • 0
    T

    '''
    public ListNode deleteDuplicates(ListNode head) {

        ListNode current = head;
        ListNode prev = null;
    
        while(current != null)
        {
            if(current.next != null && current.val == current.next.val )
            {
                ListNode del = current;
                while(current != null && current.val == del.val) current = current.next;
                if(prev == null || head == del)
                {
                    prev = current;
                    head = prev;
                }else
                {    
                    prev.next = current;
                }
            }else
            {
                 prev = current;
                 current = current.next;
            }
             
          
        }
        
        return head;
        
    }
    

    '''


Log in to reply
 

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