一个编程练习,删除单链表一个节点,且时间复杂度控制在O(1)内.

1.核心操作代码如下:

struct ListNode
{
int m_data;
ListNode *m_pNext;
}; void DeleteNode(ListNode **pListHead, ListNode *pToBeDeleted)
{
if(pListHead == NULL || pToBeDeleted == NULL)
return;
//=================================================
//删除非尾节点
if(pToBeDeleted->m_pNext != nullptr)
{
ListNode *temp = pToBeDeleted->m_pNext;
pToBeDeleted->m_data = temp->m_data;
pToBeDeleted->m_pNext = temp->m_pNext; delete temp;
temp = nullptr;
}
//=================================================
//只有一个节点 删除头
else if(pToBeDeleted == *pListHead)
{
delete pToBeDeleted;
pToBeDeleted = nullptr;
*pListHead = nullptr;
} //最后一种 删除节点是尾节点
else
{
ListNode *cur = *pListHead;
while (cur->m_pNext != pToBeDeleted)
{
cur = cur->m_pNext;
}
delete pToBeDeleted;
pToBeDeleted = nullptr;
cur->m_pNext = nullptr;
}
}

2.完整的测试实现代码如下:

头文件

#ifndef _HEAD_H_
#define _HEAD_H_ typedef int DataType; class ListNode
{
public:
ListNode(const DataType & x):m_data(x), m_pNext(NULL){} DataType m_data;
ListNode * m_pNext;
}; class Slist
{
public:
Slist():m_pHead(NULL), m_pTail(NULL)
{} ~Slist()
{
Destroy();
} void Destroy()
{
ListNode *begin =m_pHead;
while (begin)
{
ListNode *del = begin;
begin = begin->m_pNext;
delete del;
}
}
public:
//尾插法
void PushBack(const DataType &x)
{
if (m_pHead == NULL)
{
m_pHead = new ListNode(x);
m_pTail = m_pHead;
}
else
{
m_pTail->m_pNext = new ListNode(x);
m_pTail = m_pTail->m_pNext;
}
}
//查找
ListNode *find(const DataType&x)
{
ListNode *tmp = m_pHead;
while (tmp != NULL)
{
if(tmp->m_data == x)
return tmp;
else
{
tmp = tmp->m_pNext;
}
}
return NULL;
} //在O(1)时间内, 删除一个节点,函数如下:
void DeleteNodeNumone(ListNode **phead, ListNode *pToBeDelete)
{
if(*phead == nullptr || pToBeDelete == nullptr)
return; if(pToBeDelete->m_pNext != nullptr)
{
ListNode *temp = pToBeDelete->m_pNext;
pToBeDelete->m_data = temp->m_data;
pToBeDelete->m_pNext = temp->m_pNext; delete temp;
temp = nullptr;
}
//only one node
else if(*phead == pToBeDelete)
{
delete pToBeDelete;
pToBeDelete = nullptr;
*phead = nullptr;
} //删除节点是尾节点
else
{
ListNode *cur = *phead;
while (cur->m_pNext != pToBeDelete)
{
cur = cur->m_pNext;
}
delete pToBeDelete;
pToBeDelete = nullptr;
cur->m_pNext = nullptr;
}
} void print()
{
ListNode *begin = m_pHead;
while (begin)
{
cout<<begin->m_data<<"->";
begin = begin->m_pNext;
}
cout<<"NUll"<<endl;
}
public:
ListNode *m_pHead;
ListNode *m_pTail;
}; #endif //_HEAD_H_

main.cpp

int main()
{
Slist s1;
s1.PushBack();
s1.PushBack();
s1.PushBack();
s1.PushBack();
s1.PushBack();
s1.PushBack();
s1.PushBack();
s1.PushBack();
s1.print(); ListNode *num = s1.find(); s1.DeleteNodeNumone(&s1.m_pHead, num);
s1.print(); num = s1.find();
s1.DeleteNodeNumone(&s1.m_pHead, num); s1.print();
return ;
}

测试可以正常通过.

最新文章

  1. vtkQuadratic创建半球面
  2. 使用Grunt构建自动化开发环境
  3. 关于SimpleAdapter和ListView结合使用,实现列表视图的笔记
  4. 使用Django建立网站
  5. 转:最简单的基于 DirectShow 的视频播放器
  6. asp.net 微信公众平台我也来搞一搞(看看和大家的有什么不一样)
  7. 设计一个有getMin功能的栈
  8. 【转】UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)
  9. NewsDaoImpl
  10. Java并发之线程管理(线程基础知识)
  11. Python学习_05_条件、循环
  12. Web API 之承载宿主IIS,SelfHost,OwinSelfHost
  13. 例10-11 uva11181
  14. WebApi 身份认证解决方案:Basic基础认证
  15. Spring开始
  16. Postman 安装及使用入门教程 (谷歌浏览器插件版)
  17. RelativeLayout 布局参数
  18. 软工网络15团队作业4——Alpha阶段敏捷冲刺8.0
  19. mapgis IGServer账号
  20. django之中间件middleware

热门文章

  1. vue2 inheritAttrs、attrs和attrs和listeners使用
  2. C#流程控制语句--迭代语句(while,do....while, for , foreach)
  3. 手机上 input submit ios和andirod样式不统一
  4. 【C/C++】C++11 Variadic Templates
  5. 室内Vary 渲染部分作品
  6. minecraft初探
  7. mysql存储过程和执行计划案例
  8. springboot学习小记
  9. csv文件读写处理
  10. js dictionary