删除链表中等于给定值 val 的所有节点
class Solution {
public ListNode removeElements(ListNode head, int val) {
//头节点为空直接返回空
if(head == null) return null;
ListNode cur = head;
ListNode n = cur.next;
while(n!=null){
if(n.val == val){
cur.next = n.next;
n = n.next;
}else{
cur = n;
n = n.next;
}
}
//如果头节点是要删除的节点
if(head.val == val) head = head.next;
return head;
}
}