Very short 6 lines Java code, Iterative, 1 loop, 2ms


  • 1
    public ListNode removeElements(ListNode head, int val) {
        ListNode h = new ListNode(0), t=h;
        h.next = head;
        while (t.next!=null)
            if (t.next.val==val) t.next = t.next.next;
            else t = t.next;
        return h.next;
    }

Log in to reply
 

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