/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ class Solution { public: ListNode* deleteNode1(ListNode* head, int val) { if(head==nullptr) return head; ListNode* res=new ListNode(0); res->next=head; ListNode* per=res; ListNode* cur=head; while(cur!=nullptr) { if(cur->val==val) { per->next=cur->next; break; } per=cur; cur=cur->next; } return res->next; } ListNode* deleteNode(ListNode* head, int val) { if(head==nullptr) return head; ListNode* cur=head->next; ListNode* per=new ListNode(0); per->next=head; while(cur!=nullptr) { if(cur->val==val) { head->next=cur->next; break; } if(head->val==val) { per->next=per->next->next; break; } head=head->next; cur=cur->next; } return per->next; } };