剑指 Offer 18. 删除链表的节点
题目:(有改动和陷阱,不可以使用delete否则报错!!)

class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
ListNode* fhead=new ListNode(0); #设置虚拟头节点
fhead->next=head;
ListNode* cur=fhead;
while(cur->next){
if(cur->next->val==val){
ListNode* temp=cur->next;
cur->next=cur->next->next;
}
else cur=cur->next; #必须有else
}
head=fhead->next;
return head;
}
};