Easy Java recursive 1ms


  • 0
    public ListNode deleteDuplicates(ListNode head) {
            return delete(head,null);
            
        }
        public ListNode delete(ListNode head, ListNode duplicate){
            if(head==null|| (head.next==null&&duplicate==null)) 
            return head;
            ListNode next=head.next;
            
            if(next!=null){
                if(head.val==next.val) {
                    return delete(next.next,next);
                }
            }
            if(duplicate!=null){
                if(duplicate.val==head.val)
                {
                    return delete(next,null);
                }
            }
            head.next=delete(head.next,null);
            return head;
        }
    

Log in to reply
 

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