Simple Java Solution


  • 0
    Z
    public ListNode deleteDuplicates(ListNode head) {
            if(head == null || head.next == null){
                return head;
            }
    
            ListNode dummyHead = new ListNode(-1);
            ListNode tail = dummyHead;
    
            ListNode start = head;
            while (start != null){
                ListNode end = start.next;
                while (end != null && end.val == start.val){
                    end = end.next;
                }
                if(start.next == end){
                    tail.next = start;
                    tail = start;
                }
                start = end;
            }
            
            tail.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.