Topic Description: Given a linked list, no head node, how to delete one of the random node.
Analysis: If a linked list does not have a head node, then we cannot find the previous node of the deleted node. How to follow this idea, will not be able to implement the function.
Let's change the idea of what is the purpose of deleting a node. is to reach the deleted node data is deleted, that is, we actually just need to delete the node's data, not the entire node. How to achieve it. We can remove the data from the node in the following node, then assign the value to the front and delete the node. For example: ABCD three nodes, if we need to delete the B node, then we can assign C->data to B->data, let D and b connected, delete C, can.
The code is as follows:
void Deletenode (node* pcurrent)
{
if (pcurrent = = null)
return null;
node* Pnext = pcurrent->next;
if (pnext! = NULL)
{
Pcurrent->next = pnext->next;
Pcurrent->data = pnext->data;
Delete pnext;
}
}
Reference:
The beauty of programming