#include<iostream>
class Node
{
public:
int m_nValue;
Node* m_pNext;
Node() :m_pNext(nullptr){}
};
void deleteNode(Node* p_ListHead, Node* pToBeDeleted)
{
if (p_ListHead == nullptr)return;
//删除节点为中间节点,采用覆盖的形式进行删除
if (pToBeDeleted->m_pNext != nullptr)
{
Node* p_Next = pToBeDeleted->m_pNext;
pToBeDeleted->m_nValue = p_Next->m_nValue;
pToBeDeleted->m_pNext = p_Next->m_pNext;
delete p_Next;
p_Next = nullptr;
}
//删除节点为头节点
else if (pToBeDeleted == p_ListHead)
{
delete pToBeDeleted;
pToBeDeleted = nullptr;
p_ListHead = nullptr;
}
else
{//删除节点为尾节点
Node* pNode = p_ListHead;
while (pNode->m_pNext != pToBeDeleted)
{
pNode = pNode->m_pNext;
}
pNode->m_pNext = nullptr;
delete pToBeDeleted;
pToBeDeleted = nullptr;
}
}
面试题18:删除链表的节点
最新推荐文章于 2020-04-18 15:02:03 发布