/**
* Time complexity : O(N)
* Space complexity : O(1)
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
while(head != null && head.val == val) {
head = head.next;
}
if(head == null) return null;
ListNode prev = head;
ListNode curr = head.next;
while(curr != null) {
if(curr.val != val) {
prev.next = curr;
prev = prev.next;
}
curr = curr.next;
}
prev.next = null;
return head;
}
}