由于链表只能指向下一个节点,需要一个单独指针指向前一个节点,连接前后节点即可删除该节点
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
if(head->val==val)return head->next;
ListNode* out=head;
auto pre=out;
head=head->next;
while(head){
if(head->val==val){
pre->next=head->next;
return out;
}
pre=head;
head=head->next;
}
return out;
}
};



