Create a ListNode dummy
, where dummy.next = head
. This will make it easier to remove elements at the head of the list, if necessary.
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode n = dummy;
while (n.next != null) {
if (n.next.val == val) {
n.next = n.next.next;
} else {
n = n.next;
}
}
return dummy.next;
}
}
- Time Complexity: O(n)
- Space Complexity: O(1)