1 ms Java solution


  • 0
    C
    class Solution {
        public ListNode removeElements(ListNode head, int val) {
            if (head == null) return head;
            ListNode cur = new ListNode(0);
            ListNode newList = new ListNode(0);
            cur.next = head;
            newList = cur;
            while(cur.next != null) {
                if (cur.next.val == val) cur.next = cur.next.next;
                else cur = cur.next;
            }
            return newList.next;
        }
    }
    

Log in to reply
 

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