Simple recursive solution java 10 lines


  • 0
    V
    public class Solution {
    public ListNode removeElements(ListNode head, int val) {
         if(head==null)
            return head;
        if(head.next==null)
            return head.val==val?null:head;
        while(head!=null&&head.val==val)
            head=head.next;
        if(head==null)
            return head;
        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.