![在O(1) 时间删除链表节点 在O(1) 时间删除链表节点](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
struct Node {
int val;
Node * next;
};
void deleteNode(Node ** head, Node * target)
{
assert(head != NULL && *head != NULL && target != NULL); //头结点的判断
if(*head == target)
{
*head = target->next;
delete target;
} //尾节点的判断
if(target->next == NULL){ Node *p = *head;
while(*p ->next != head)
p = p->next; p->next = NULL;
delete target ;
}else{ Node *p = target-next;
target->val = p->val;
target->next = p->next;
delete p;
}
}