Note:

Key word:

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

Complexity Analysis:

TC: O(N);

SC: O(1);