删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5
public ListNode removeElements(ListNode head, int val) {
//返回的ListNode
ListNode res=new ListNode(0);
//用于储存上一个的ListNode
ListNode up= res;
res.next=head;
while(head!=null){
if(head.val==val){
head = delect(up);
}else {
up = head;
head=head.next;
}
}
return res.next;
}
public ListNode delect(ListNode zrohead) {
if (zrohead.next.next != null) {
ListNode ne = zrohead.next.next;
zrohead.next = null;
zrohead.next = ne;
} else {
zrohead.next = null;
}
return zrohead.next;
}