题目描述:
Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
题目简单,一次ac,代码如下:
public ListNode removeElements(ListNode head, int val) {
ListNode start=new ListNode(-1);
start.next=head;
ListNode pre=start,cur=head;
while(cur!=null){
if(cur.val==val){
pre.next=cur.next;
}else{
pre=pre.next;
}
cur=cur.next;
}
return start.next;
}