删除链表中等于给定值 val 的所有节点
public class Solution {
public static Node removeElements(Node head,int val){
//如果删除的是头结点要单独考虑
while(head != null && head.val == val){
head = head.next;
}
if(head ==null){
return head;
}
Node prev = head;
while(prev.next != null ){
if(prev.next.val == val){
prev.next = prev.next.next;
}else{
prev = prev.next;
}
}
return head;
}
}