Java concise solution: 1ms+O(N)+only one while() loop


  • 1
    J
        public ListNode deleteDuplicates(ListNode head) {
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            if(head == null || head.next == null) return head;
            ListNode start = dummy, pre = head, cur = head.next;
            int count = 1;
            while(cur != null){
                if(pre.val == cur.val){
                    count++;
                }else if(count > 1){ 
                    start.next = cur;
                    count = 1;
                }else{
                    start = pre;
                }
                pre = cur;
                cur = cur.next;
            }
            if(count > 1) start.next = cur;
            return dummy.next;
        }

Log in to reply
 

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