Clean Java solution


  • 11
    B
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null||head.next==null) return head;
        ListNode dummy=head;
        while(dummy.next!=null){
            if(dummy.next.val==dummy.val){
                dummy.next=dummy.next.next;
            }else dummy=dummy.next;
        }
        return head;
    }

  • 3
    V
    public ListNode deleteDuplicates(ListNode head) {
            ListNode current = head;
            while(current!=null && current.next!=null)
                if(current.next.val>current.val) current = current.next;
                else current.next = current.next.next;
            return head;
    }

  • 0
    R

    Why "while(head.next!=null)" has NullPointerException but "while(dummy.next!=null)" does not?


Log in to reply
 

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