题目:(有改动和陷阱,不可以使用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;
}
};
标签:fhead,head,ListNode,cur,val,Offer,18,next,链表
From: https://www.cnblogs.com/fly-smart/p/17569875.html