simple java solution


  • 0
    L
    public ListNode removeElements(ListNode head, int val) {
         ListNode dummyHead = new ListNode(-1);
         ListNode it = dummyHead;
         
         ListNode node = head;
         while(node!=null) {
             if(node.val!=val) {
                 it.next = node;
                 it = it.next;
             }
             node = node.next;
         }
         it.next = null; //important to not copy the rest.
         return dummyHead.next;
        }
    
    

Log in to reply
 

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