题目:(有改动和陷阱,不可以使用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;
    }
};
posted on 2023-07-20 22:23  孜孜不倦fly  阅读(10)  评论(0)    收藏  举报