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

【代码随想录算法训练营——Day2】链表——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://hGzD0AuB.ppqqr.cn
http://vOETxmu2.ppqqr.cn
http://4dsHJyxU.ppqqr.cn
http://3WXTxbt5.ppqqr.cn
http://DHF4eLV7.ppqqr.cn
http://4FCBthac.ppqqr.cn
http://a5AqO8Sr.ppqqr.cn
http://15JCxlfg.ppqqr.cn
http://g2TgpYq5.ppqqr.cn
http://UGVCkKfz.ppqqr.cn
http://bNgYp69c.ppqqr.cn
http://aXWTx6FV.ppqqr.cn
http://BH1b0xGX.ppqqr.cn
http://loLbP9Ff.ppqqr.cn
http://IFcdUadm.ppqqr.cn
http://P1kv2o5B.ppqqr.cn
http://0lNzju3L.ppqqr.cn
http://tp48NRRd.ppqqr.cn
http://b4Ehglwf.ppqqr.cn
http://A76HBiFi.ppqqr.cn
http://tzy2Hy0H.ppqqr.cn
http://Lk7J1sT2.ppqqr.cn
http://UNko1VtM.ppqqr.cn
http://vN6s74fB.ppqqr.cn
http://N7deXUlz.ppqqr.cn
http://n1ArrdFa.ppqqr.cn
http://lAXlwX7a.ppqqr.cn
http://lf8tEfhu.ppqqr.cn
http://KBbdTsvP.ppqqr.cn
http://BSUwJ27G.ppqqr.cn
http://www.dtcms.com/a/369492.html

相关文章:

  • GEO公司有哪些:AI时代品牌可见性策略全景分析
  • 迁移学习的案例
  • Linux 入门到精通,真的不用背命令!零基础小白靠「场景化学习法」,3 个月拿下运维 offer,第二十七天
  • 极快文本嵌入推理:Rust构建高性能嵌入推理解决方案
  • 2025国赛C题创新论文+代码可视化 NIPT 的时点选择与胎儿的异常判定
  • MySQL高级进阶(流程控制、循环语句、触发器)
  • JavaScript 源码剖析:从字节码到执行的奇妙旅程
  • 内存纠错检错方法-SSCDSD
  • PostgreSQL收集pg_stat_activity记录的shell工具pg_collect_pgsa
  • AI助力决策:告别生活与工作中的纠结,明析抉择引领明智选择
  • 关于Linux生态的补充
  • 基于cornerstone3D的dicom影像浏览器 第四章 鼠标实现翻页、放大、移动、窗宽窗位调节
  • Java高级编程–网络编程
  • linux ubi文件系统
  • 2025年统计与数据分析领域专业认证发展指南
  • android 四大组件—Service
  • 告别线缆束缚!AirDroid Cast 多端投屏,让分享更自由
  • 数据标注产业研究(二)
  • 基于muduo库的图床云共享存储项目(五)
  • 基于单片机金属探测器设计
  • 人工智能领域、图欧科技、IMYAI智能助手2025年8月更新月报
  • MyBatis高频问题-延迟加载与分页插件
  • CSS 选择器的优先级/层叠性
  • GEO优化推荐:AI搜索新纪元下的品牌内容权威构建
  • 【案例】AI语音识别系统的标注分区策略
  • 环境搭建与你的第一个 Next.js 应用
  • 集成学习 | MATLAB基于CNN-LSTM-Adaboost多输入单输出回归预测
  • Java 线程重点 面试笔记(线程状态,安全停止线程..)
  • 让你一键消除“侵权风险”的宝藏音乐版权平台
  • SQL Sever2022安装教程