class Solution {
public ListNode removeElements(ListNode head, int val){
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode current = dummy;
while(current.next != null){
if(current.next.val == val) current.next = current.next.next;
else current = current.next;
}
return dummy.next;
}
}
Explanation: Dummy node helps handle head removal. Iterate and remove matching nodes.
β± O(n) | πΎ O(1)