Q:移除链表中值为val的元素,返回新的链表
for example:
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
public ListNode removeElements(ListNode head, int val) {
ListNode newhead=new ListNode(-1);
newhead.next=head;
ListNode cur=newhead;//只用了一个指针
while(cur.next!=null){
if(cur.next.val==val)
{
cur.next=cur.next.next;
}
else
cur=cur.next;
}
return newhead.next;
}