删除链表当中指定key的项 链表的遍历与删除操作
唯一需要注意的是,删除head节点和删除其他节点不一样
</pre><pre name="code" class="java">/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode cur, pre;
pre = null;
cur = head;
while(cur != null)
{
//判断KEY
if(cur.val == val){
if(cur == head){
head = cur.next;
cur = head;
}
else{
pre.next = cur.next;
cur = pre.next;
}
}else{
pre = cur;
cur = cur.next;
}
}
return head;
}
}