https://leetcode-cn.com/problems/remove-linked-list-elements/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode ans=head;
if (head==null){
return head;
}
while (head!=null){
if (head.val==val){
ans=head.next;
head=head.next;
}else {
if(head.next==null){
break;
}
if (head.next.val==val){
head.next=head.next.next;
}else {
head=head.next;
}
}
}
return ans;
}
}