当前位置: 首页 > news >正文

【代码随想录算法训练营——Day3】链表——203.移除链表元素、707.设计链表、206.反转链表

LeetCode题目链接
https://leetcode.cn/problems/remove-linked-list-elements/
https://leetcode.cn/problems/design-linked-list/
https://leetcode.cn/problems/reverse-linked-list/

题解
203.移除链表元素
重要的是创立头结点,这点在写题前已经经受过提示。
注意移除代码中pre结点创建后,在循环中会跟着head往下走,因此要再创建一个cur结点,并令其初值等于pre,而不是直接令其next等于head,因为在遍历过程中移动了next指针,头结点被删除后可能在cur的next里没被改变,因此要直接令其值等于pre,来改变其next。
其他的创建链表代码有借鉴AI的(实在是AI有提示,没办法)。

707.设计链表
借助AI的帮助下写了几行(不是很多),主要是在按下标添加结点时,index长度等于链表长度时调用了addAtTail(val),这点比较要注意,其他的只是模拟。

206.反转链表
在最后return环节那里,是AI给我提示的(没办法,AI太快了),脑中模拟一下就知道是pre返回,这回完全没看题解,都是凭之前的印象做出的,第一遍刷题真的有用!

代码

//203.移除链表元素
#include <iostream>
#include <vector>
using namespace std;struct ListNode {int val;ListNode *next;ListNode() : val(0), next(nullptr) {}ListNode(int x) : val(x), next(nullptr) {}      ListNode(int x, ListNode *next) : val(x), next(next) {}
};class Solution {
public:ListNode* removeElements(ListNode* head, int val) {if(head == NULL) return NULL;ListNode* pre = new ListNode;pre->next = head;ListNode* cur = pre;while (head != NULL) {if (head->val == val) {pre->next = head->next;head = head->next;}else {pre = head;head = head->next;}}return cur->next;}
};ListNode* createList(vector<int> nums) {if (nums.size() == 0) return NULL;ListNode *pre = new ListNode;pre->next = NULL;ListNode* cur = pre;for (int i = 0;i < nums.size();i++) {cur->next = new ListNode(nums[i]);cur = cur->next;}cur->next = NULL;return pre->next;
}int main() {Solution s;vector<int> nums = {  };ListNode* head = createList(nums);int val = 1;ListNode* node = head;while (node != NULL) {printf("%d ", node->val);node = node->next;}printf("\n");ListNode* result = s.removeElements(head, val);node = result;while (node != NULL) {printf("%d ", node->val);node = node->next;}return 0;
}
//707.设计链表
#include <iostream>
using namespace std;class MyLinkedList {
public:struct ListNode {int val;ListNode* next;ListNode() : val(0), next(nullptr) {}ListNode(int x) : val(x), next(nullptr) {}};;MyLinkedList() {preHead = new ListNode();}int getListLen() {ListNode* head = preHead->next;int len = -1;while (head != NULL) {len++;head = head->next;}return len;}int get(int index) {int len = getListLen();if (index < 0 || index > len) return -1;int num = -1;ListNode* head = preHead->next;while (head != NULL) {num++;if (num == index) {return head->val;}head = head->next;}return -1;}void addAtHead(int val) {ListNode* node = new ListNode(val);node->next = preHead->next;preHead->next = node;}void addAtTail(int val) {ListNode* pre = preHead;ListNode* head = preHead->next;while (head != NULL) {pre = head;head = head->next;}ListNode* node = new ListNode(val);pre->next = node;}void addAtIndex(int index, int val) {int len = getListLen() + 1;if (index > len) return;if (index == len) {addAtTail(val); //妙啊return;}ListNode* pre = preHead;ListNode* head = preHead->next;int num = -1;while (num < index - 1) {num++;pre = head;head = head->next;}ListNode* node = new ListNode(val);node->next = pre->next;pre->next = node;}void deleteAtIndex(int index) {int len = getListLen();if (index < 0 || index > len) return;ListNode* pre = preHead;ListNode* head = preHead->next;int num = 0;while (num < index) {pre = head;head = head->next;num++;}pre->next = head->next;delete head;}
private:ListNode* preHead;
};int main() {MyLinkedList myLinkedList;myLinkedList.addAtHead(1);myLinkedList.addAtTail(3);myLinkedList.addAtIndex(1, 2);    // 链表变为 1->2->3printf("%d\n", myLinkedList.get(1));              // 返回 2myLinkedList.deleteAtIndex(1);    // 现在,链表变为 1->3printf("%d\n", myLinkedList.get(1));              // 返回 3return 0;
}
//206.反转链表
#include <iostream>
#include <vector>
using namespace std;struct ListNode {int val;ListNode *next;ListNode() : val(0), next(nullptr) {}ListNode(int x) : val(x), next(nullptr) {}ListNode(int x, ListNode *next) : val(x), next(next) {}};class Solution {
public:ListNode* reverseList(ListNode* head) {if (head == NULL) return NULL;ListNode* pre = NULL;while (head != NULL) {ListNode* tmp = head->next;head->next = pre;pre = head;head = tmp;}return pre;}
};ListNode* createList(vector<int> nums) {if (nums.size() == 0) return NULL;ListNode* pre = new ListNode;pre->next = NULL;ListNode* cur = pre;for (int i = 0;i < nums.size();i++) {cur->next = new ListNode(nums[i]);cur = cur->next;}cur->next = NULL;return pre->next;
}int main() {vector<int> nums = {  };ListNode* head = createList(nums);ListNode* node = head;while (node != NULL) {printf("%d ", node->val);node = node->next;}printf("\n");Solution s;ListNode* result = s.reverseList(head);node = result;while (node != NULL) {printf("%d ", node->val);node = node->next;}return 0;
}

文章转载自:

http://KjiASodK.mpyry.cn
http://BBkFhFrE.mpyry.cn
http://zcmyDtE9.mpyry.cn
http://Rc3Vql8E.mpyry.cn
http://UAVsEaIS.mpyry.cn
http://TlolKS1N.mpyry.cn
http://faphYbX1.mpyry.cn
http://8Df92fEB.mpyry.cn
http://NMa9D8N1.mpyry.cn
http://2wlBuYS9.mpyry.cn
http://Xp1BmpGA.mpyry.cn
http://Iwpq7M7y.mpyry.cn
http://ZfLP37zM.mpyry.cn
http://h9HaKqxL.mpyry.cn
http://8WptT1dL.mpyry.cn
http://rx2kyNOt.mpyry.cn
http://LRIRiAgq.mpyry.cn
http://3adOyyEv.mpyry.cn
http://S9Ltr3Iq.mpyry.cn
http://1o7ZK6MW.mpyry.cn
http://mKZqPaK9.mpyry.cn
http://3ubqq4IW.mpyry.cn
http://0MPjHVBa.mpyry.cn
http://VhsPwpLr.mpyry.cn
http://G5vg7bdi.mpyry.cn
http://Lf9MxbPL.mpyry.cn
http://rXQDzGD3.mpyry.cn
http://ppOZ0D7p.mpyry.cn
http://fjBZgTXF.mpyry.cn
http://nTZwWGwn.mpyry.cn
http://www.dtcms.com/a/369694.html

相关文章:

  • 目标检测双雄:一阶段与二阶段检测器全解析
  • 2025高教社数学建模国赛C题 - NIPT的时点选择与胎儿的异常判定(完整参考论文)
  • keil 5 STM32工程介绍
  • C/C++包管理工具:Conan
  • 标注格式转换csv转xml
  • 错误是ModuleNotFoundError: No module named ‘pip‘解决“找不到 pip”
  • 文章采集发布帝国ECMS网站技巧
  • 创新、绿色、共赢:芬兰企业在华发展战略与案例解析(2025中芬建交75周年)
  • PAIN | 痛在你身,激活在我脑:原来后侧默认模式网络是‘感同身受’的神经开关
  • 【C++】Vector完全指南:动态数组高效使用
  • 状压 dp --- TSP 问题
  • 【数字孪生核心技术】什么是倾斜摄影?
  • 公共卫浴感应开关选红外还是雷达
  • 解决 Apache/WAF SSL 证书链不完整导致的 PKIX path building failed 问题
  • 计算机二级C语言操作题(填空、修改、设计题)——真题库(17)附解析答案
  • 上位机通信基础知识
  • Acrobat-2025.001.20643_Win中文_PDF编辑器_便携版安装教程
  • Java基础 9.5
  • javafx笔记
  • 大基座模型与 Scaling Law:AI 时代的逻辑与困境
  • 扩展与改进的密钥协商协议
  • Spring整合MQTT使用
  • AI应用开发-技术架构 PAFR介绍
  • 9月5日星期五今日早报简报微语报早读
  • Zynq-7000 上 RT-Thread 的 MMU 与 SMP 优势分析
  • 【完整源码+数据集+部署教程】西兰花实例分割系统源码和数据集:改进yolo11-AggregatedAtt
  • 数据库查询优化
  • PiscCode基于 Mediapipe 实现轨迹跟踪
  • 硬件(三) 通信方式、串口通信
  • 在 CentOS 上完整安装 Docker 指南