Recursive Solution


  • 0
    P

    Move head forwards every time match the val.

        public ListNode removeElements(ListNode head, int val) {
            
            if(head == null){
                return null;
            }
    
            while (head != null && head.val == val){
                head = head.next;
            }
    
            if (head != null){
                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.