3ms Java recursive


  • 0
    public ListNode removeElements(ListNode head, int val) {
        if(head==null) return null;
        else if(head.val==val) return removeElements(head.next,val);
        if(head.next!=null){
        if(head.next.val==val) head.next=removeElements(head.next.next,val);
                else head.next=removeElements(head.next,val);
           }
             return head;  
        }
    

Log in to reply
 

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