删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5
package com.binglian.Linked;
public class LK_203 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode removeElements(ListNode head, int val) {
//需要对头节点进行特殊处理
while(head !=null && head.val ==val){
ListNode node=head;
head=head.next;
}
if(head == null){
return head;
}
ListNode cur=head;
while(cur.next !=null){
if(cur.next.val ==val){
ListNode deListNode=cur.next;
cur.next=deListNode.next;
}else{
cur=cur.next;
}
}
return head;
}
}