在O(1)时间删除链表结点

//给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点。
1 struct ListNode //结点结构
{
int m_nValue;
ListNode* m_pNext;
}; void DeleteNode(ListNode** pListHead , ListNode* pToBeDeleted)
{
if (!pListHead || !pToBeDeleted)
{
return;
}
if (pToBeDeleted->m_pNext != NULL)
{
ListNode* pNext = pToBeDeleted->m_pNext ;
pToBeDeleted->m_nValue = pNext->m_nValue;
pToBeDeleted->m_pNext = pNext->m_pNext;
delete pNext;
pNext = NULL;
}
else if (*pListHead == pToBeDeleted)//只有一个结点
{
23 delete pToBeDeleted;
pToBeDeleted = NULL;
*pListHead = NULL;
}
else //尾结点
{
ListNode* pNode = *pListHead ;
while (pNode->m_pNext != pToBeDeleted)
{
pNode = pNode->m_pNext;
}
pNode->m_pNext = NULL ;
delete pToBeDeleted;
pToBeDeleted = NULL ;
}
}
上一篇:ASP.NET MVC下的异步Action的定义和执行原理


下一篇:KestrelServer