Super simple Java solution


  • -1
    A
     public class Solution {
        public ListNode removeElements(ListNode head, int val) {
              
              if(head == null){
                  return head;
              }
    
            while(head.val == val){
                head = head.next;
                if(head == null){
                    return head;
                }
            }
            
            ListNode prev = head;
            
            while(prev.next != null){
                if(prev.next.val == val){
                    prev.next = prev.next.next;
                } else {
                    prev = prev.next;
                }
            }
            return head;
        }
    }

Log in to reply
 

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