Problem
Remove all elements from a linked list of integers that have value val.
Example
Given 1->2->3->3->4->5->3
, val = 3, you should return the list as 1->2->4->5
Solution
public class Solution {
public ListNode removeElements(ListNode head, int val) {
// Write your code here
ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy;
while (head.next != null) {
if (head.next.val == val) {
head.next = head.next.next;
}
else {
head = head.next;
}
}
return dummy.next;
}
}