Problem Description: the head pointer and a node pointer to the order-linked list, defining a function to delete the node at O (1) time.
This is relatively simple, do not explain, directly read the reference code, but one thing is to note, or to see the deleted node type, can not guarantee always O (1) time
Reference code:
void Deletenode (listnode** phead,listnode *ptobedelete)
{
if ((Phead = = NULL) | | (*phead = = NULL) | | (Ptobedelete = = NULL))
{
Return
}
The node in the middle of the linked list is deleted
if (ptobedelete->m_pnext! = NULL)
{
ListNode *pnode = ptobedelete->m_pnext;
Ptobedelete->m_nvalue = pnode->m_nvalue;
Ptobedelete->m_pnext = pnode->m_pnext;
Delete Pnode;
Pnode = NULL;
}
Else
{//Only one node
if (Ptobedelete = = *phead)
{
Delete *phead;
Phead = NULL;
Ptobedelete = NULL;
}
Else
{//Deleted tail node
ListNode *pnode = *phead;
while (pnode->m_pnext! = ptobedelete)
{
Pnode = pnode->m_pnext;
}
Pnode->m_pnext = NULL;
Delete Ptobedelete;
Ptobedelete = NULL;
}
}
}
O (1) time to delete a linked list node