Standard LinkedList dummy solution! very concise!


  • -2
    P
             public ListNode removeElements(ListNode head, int val){
                 if(head == null)
                 return null;
                 ListNode dummy = new ListNode(-1);
                 dummy.next = head;
                 head = dummy;
                 while(head.next != null){
                 if(head.val == val){
                 head = head.next.next;
                 }
                else{
                     head = head.next;
                 }
          
                 return dummy.next;
       }

Log in to reply
 

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