剑指offer之C++语言实现链表(两种删除节点方式)

简介: 剑指offer之C++语言实现链表(两种删除节点方式)

1 问题

用C++语言实现链表

2 代码实现

#include <iostream>
#include <stdlib.h>
using namespace std;
class List
{
public:
  List();
  ~List();
  List* createNode(int value);//创建节点
  bool insertNode(List *node);//插入节点
  void printList();//打印节点
  bool deleteNode(List *node);//删除节点不移动头节点
  bool deleteNode1(List *node);//删除节点移动头节点
  int listSize();//长度
  void printNode();//打印但前的value
  void freeList();//释放链表
private:
  int value;
  List *head;
  List *next;
};
bool List::deleteNode(List *node)
{
  if (node == NULL)
  {
    std:cout << "node is NULL" << std::endl;  
    return false;
  }
  if (head == NULL)
  {
    std::cout << "head is NULL" << std::endl; 
    return false;
  }
  //如果node等于head
  if (head == node)
  {
    head = head->next;
  }
  List *p = head;
  while (p->next != NULL)
  {
    if (p->next == node)
    {
      p->next = p->next->next;
      return true;
    }
    p = p->next;
  }
  return false; 
}
bool List::deleteNode1(List *node)
{
  if (node == NULL)
  {
    std:cout << "node is NULL" << std::endl;  
    return false;
  }
  if (head == NULL)
  {
    std::cout << "head is NULL" << std::endl; 
    return false;
  }
  //如果node等于head
  if (head == node)
  {
    head = head->next;
  }
  List *p = head;
  while (head->next != NULL)
  {
    if (head->next == node)
    {
      head->next = head->next->next;
      std::cout << "delete node success head->value" << head->value << std::endl;
      //这里要记得把头节点的指针移动最后还原,这里的头节点是保存在这个类里面,改变了就是改变了
      //如果这里是把head作为参数传递,最后head会被销毁那么不需要移动头指针
      head = p;
      return true;
    }
    //注意,这里由于head是成员变量,改变了就是改变了,所以需要最后重新指定
    head = head->next;
  }
  std::cout << "delete node fail head->value" << head->value << std::endl;
  //这里要记得把头节点的指针移动最后还原,这里的头节点是保存在这个类里面,改变了就是改变了
  //如果这里是把head作为参数传递,最后head会被销毁那么不需要移动头指针
  head = p;
  return false; 
}
List::List()
{
  value = 0;
  head = NULL;
  next = NULL;
}
List::~List()
{
  delete head;
  delete next;
}
List* List::createNode(int value)
{
  List *list = NULL;
  list = new List();
  if (list)
  {
    list->value = value;
    return list;  
  }
  return NULL;
}
bool List::insertNode(List *node)
{
  node->next = head;
  head = node;
  return true;  
}
void List::printList()
{ if (head == NULL)
  {
    std::cout << "head is NULL" << std::endl;
    return;
  }
  List *p = head;
  while (p != NULL)
  {
    std::cout << p->value << std::endl; 
    p = p->next;
  }
  return; 
}
void List::printNode()
{
  std::cout << value << std::endl;  
}
int List::listSize()
{
  if (head == NULL)
  {
    std::cout << "head is NULL" << std::endl;
    return 0; 
  }
  int len = 0;
  List *p = head;
  while (p != NULL)
  {
    p = p->next;
    ++len;
  }
  return len;
}
void List::freeList()
{
  if (head == NULL)
  {
    std::cout << "head is NULL" << std::endl;
    return; 
  }
  List *p;
  while (head != NULL)
  {
    p = head;
    head = head->next;
    free(p);
  }
}
int main()
{
  List list;
  List *list1 = list.createNode(5);
  list.insertNode(list1);
  List *list2 = list.createNode(6);
  list.insertNode(list2);
  List *list3 = list.createNode(1);
  list.insertNode(list3);
  List *list4 = list.createNode(3);
  list.insertNode(list4);
  List *list5 = list.createNode(2);
  list.insertNode(list5);
  list.printList();
  std::cout << "list size is " << list.listSize() << std::endl;
  std::cout << "-----------开始删除节点值为3的节点" << std::endl;
  list.deleteNode1(list4);
  list.printList();
  std::cout << "list size is " << list.listSize() << std::endl;
  list.freeList();
  list.printList();
  return 0; 
}

3 运行结果

2
3
1
6
5
list size is 5
-----------开始删除节点值为3的节点
delete node success head->value2
2
1
6
5
list size is 4
head is NULL
相关文章
|
6月前
|
机器学习/深度学习 算法
24. 两两交换链表中的节点, 19.删除链表的倒数第N个节点 ,面试题 02.07. 链表相交
1. **两两交换链表中的节点**:通过引入虚拟头结点,使所有节点都能采用统一的交换逻辑,避免对头结点单独处理。 2. **删除链表的倒数第N个节点**:利用双指针技巧,让快慢指针保持N个节点的距离,当快指针到达末尾时,慢指针正好指向待删除节点的前一个节点。 3. **链表相交**:先计算两链表长度并调整起点,确保从相同距离末尾的位置开始遍历,从而高效找到相交节点或确定无交点。 以上方法均在时间复杂度和空间复杂度上进行了优化,适合用于理解和掌握链表的基本操作及常见算法设计思路。
|
8月前
|
存储 C++
UE5 C++:自定义Http节点获取Header数据
综上,通过为UE5创建一个自定义HTTP请求类并覆盖GetResult方法,就能成功地从HTTP响应的Header数据中提取信息。在项目中使用自定义类,不仅可以方便地访问响应头数据,也可随时使用这些信息。希望这种方法可以为你的开发过程带来便利和效益。
303 35
LeetCode第二十四题(两两交换链表中的节点)
这篇文章介绍了LeetCode第24题的解法,即如何通过使用三个指针(preNode, curNode, curNextNode)来两两交换链表中的节点,并提供了详细的代码实现。
151 0
LeetCode第二十四题(两两交换链表中的节点)
Leetcode第十九题(删除链表的倒数第N个节点)
LeetCode第19题要求删除链表的倒数第N个节点,可以通过快慢指针法在一次遍历中实现。
195 0
Leetcode第十九题(删除链表的倒数第N个节点)
LeetCode第24题两两交换链表中的节点
这篇文章介绍了LeetCode第24题"两两交换链表中的节点"的解题方法,通过使用虚拟节点和前驱节点技巧,实现了链表中相邻节点的交换。
LeetCode第24题两两交换链表中的节点
(剑指offer)18、删除链表的节点—22、链表中倒数第K个节点—25、合并两个排序的链表—52、两个链表的第一个公共节点(2021.12.07)
(剑指offer)18、删除链表的节点—22、链表中倒数第K个节点—25、合并两个排序的链表—52、两个链表的第一个公共节点(2021.12.07)
175 0
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
201 5
|
Python
【Leetcode刷题Python】剑指 Offer 18. 删除链表的节点
Leetcode题目"剑指 Offer 18. 删除链表的节点"的Python解决方案,通过使用双指针法找到并删除链表中值为特定数值的节点,然后返回更新后的链表头节点。
200 4

热门文章

最新文章