链表的插入、删除、打印、删除倒数第N个节点

复制代码
  1 #include <iostream>
  2 #include <vector>
  3 
  4 using namespace std;
  5 
  6 class ListNode {
  7 public:
  8     ListNode() : val(0), next(nullptr) {}
  9     ListNode(int x) : val(x), next(nullptr) {}
 10     ListNode(int x, ListNode *next) : val(x), next(next) {}
 11 
 12 public:
 13     int val;
 14     ListNode *next;
 15 };
 16 
 17 class Solution {
 18 public:
 19     // 删除链表中倒数第N个节点
 20     void removeNthFromEnd(ListNode *&head, int n) {
 21         ListNode *fast = head;
 22         ListNode *slow = head;
 23         //慢指针比快指针慢N步,那么快指针指向末尾的null时,慢指针刚好指向要删除结点的前驱结点
 24         while (fast->next != nullptr) {
 25             fast = fast->next;
 26             if (n == 0) {
 27                 slow = slow->next;
 28             } else {
 29                 n--;
 30             }
 31         }
 32         ListNode *curNode;
 33         if (n != 0) { //没追上,说明删除的是头指针
 34             curNode = head;
 35             head = head->next;
 36         } else {
 37             curNode = slow->next;
 38             slow->next = slow->next->next;
 39         }
 40         delete curNode;
 41         return;
 42     }
 43     // 尾插法插入链表元素
 44     void insertNodeAtTail(ListNode *&head, int val) {
 45         ListNode *newNode = new ListNode(val);
 46         ListNode *currentNode = head;
 47         if (head == nullptr) {
 48             head = newNode;
 49             return;
 50         }
 51         while (currentNode->next != nullptr) {
 52             currentNode = currentNode->next;
 53         }
 54         currentNode->next = newNode;
 55         return;
 56     }
 57     // 打印链表元素
 58     void printfListNode(ListNode *head) {
 59         while (head != nullptr) {
 60             std::cout << head->val << "->";
 61             head = head->next;
 62         }
 63         std::cout << "NULL" << endl;
 64         return;
 65     }
 66     // 删除链表中给定值节点
 67     void removeNode(ListNode *&head, int val) {
 68         ListNode *pre = head;
 69         ListNode *current = head;
 70         if (head == nullptr) {
 71             return;
 72         }
 73         while (current->next != nullptr && current->val != val) {
 74             pre = current;
 75             current = current->next;
 76         }
 77         if (current->val == val) {
 78             if (current == head) {
 79                 head = current->next;
 80             } else {
 81                 pre->next = current->next;
 82             }
 83             delete current;
 84         }
 85         return;
 86     }
 87     // 获取当前要删除值得节点指针
 88     ListNode *getRemoveNode(ListNode *head, int val) {
 89         ListNode *current = head;
 90         if (head == nullptr) {
 91             return nullptr;
 92         }
 93         while (current->next != nullptr) {
 94             if (current->val == val) {
 95                 return current;
 96             }
 97             current = current->next;
 98         }
 99         if (current->val == val) {
100             return current;
101         }
102         return nullptr;
103     }
104 };
105 int main()
106 {
107     Solution *test = new Solution();
108     vector<int> vec = {1, 2, 3, 4, 5, 6, 7 };
109     ListNode *head = nullptr;
110     // 插入节点
111     for (const auto &v : vec) {
112         test->insertNodeAtTail(head, v);
113     }
114     test->printfListNode(head); // 1->2->3->4->5->6->7->NULL
115     // 删除头结点
116     test->removeNode(head, 1);
117     test->printfListNode(head); // 2->3->4->5->6->7->NULL
118     // 删除尾结点
119     test->removeNode(head, 7);
120     test->printfListNode(head); // 2->3->4->5->6->NULL
121     // 删除倒数第N个节点
122     test->removeNthFromEnd(head, 1);
123     test->printfListNode(head); // 2->3->4->5->NULL
124     test->removeNthFromEnd(head, 4);
125     test->printfListNode(head); // 3->4->5->NULL
126     test->removeNthFromEnd(head, 2);
127     test->printfListNode(head); // 3->5->NULL
128     delete test;
129     system("pause");
130     return 0;
131 }
复制代码

运行效果:

 

posted @   跳动的休止符  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示