题目描述:
Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
代码:
class Solution {
public ListNode removeElements(ListNode head, int val) {
for(ListNode pre=head,x=head;x!=null;x=x.next){
if(x.val==val){
if(x==head){
head=head.next;
}else{
pre.next=x.next;
}
}else{
pre=x;//不能换成pre=pre.next
}
}
return head;
}
}