Share my clear java solution by recursion


  • 1
    C
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null || head.next==null) return head;
        if(head.val != head.next.val) head.next = deleteDuplicates(head.next);
        else{
            int nextNum = head.next.val;
            while(head != null && head.val == nextNum)
                head = head.next;
            head = deleteDuplicates(head);
        }
        return head;
    }

Log in to reply
 

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