虚拟结点法刷链表类的题,经常会用到,尤其是对与链表头节点的处理,一定要记住! ! !
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode pHead = new ListNode(-1);
pHead.next = head;
ListNode cur = pHead;
while(cur!=null&&cur.next!=null){
if(cur.next.val == val){
cur.next=cur.next.next;
}else{
cur= cur.next;
}
}
return pHead.next;
}
}