Share my Java solution


  • 0
    W
    public ListNode deleteDuplicates(ListNode head) {
        ListNode start = new ListNode(0);
        ListNode pre = start;
        start.next = head;
        
        
        while(head != null) {
            if(head.next == null) break;
            if(head.val == head.next.val) {
                while(head.next != null && head.val == head.next.val) head = head.next;
                head = head.next;
            } else {
                pre.next = head;
                pre = head;
                head = head.next;
            }
        }
        pre.next = head;
        return start.next;
    }

Log in to reply
 

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