Another Java Recursive Solution


  • 0
    D

    '''

    private ListNode helper(ListNode head, int preVal){
        if(head == null) return null;
        if(head.next == null ){
            if(head.val == preVal) return null;
            return head;
        }
        if(preVal != head.val && head.val != head.next.val){ 
            head.next = help(head.next, head.val);
            return head;
        }
        return help(head.next, head.val);
    }
    
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null) return head;
        return help(head, head.val-1);
    }
    

    '''


Log in to reply
 

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