题目:(有改动和陷阱,不可以使用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,否则cur->next这个节点的判断会被跳过
}
head=fhead->next;
return head;
}
};
浙公网安备 33010602011771号