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

[豪の算法奇妙冒险] 代码随想录算法训练营第三天 | 203-移除链表元素、707-设计链表、206-反转链表

代码随想录算法训练营第三天 | 203-移除链表元素、707-设计链表、206-反转链表


LeetCode203 移除链表元素

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

文章讲解:https://programmercarl.com/0203.移除链表元素.html

视频讲解:https://www.bilibili.com/video/BV18B4y1s7R9/?vd_source=b989f2b109eb3b17e8178154a7de7a51

​ 引入虚拟头节点,这样可以使用一套规则去删除链表中节点,处理起来更加方便

​ 如果是C++还得额外考虑内存回收,但我用的是Java,虚拟机自动回收内存hh

image-20251121152717416

class Solution {public ListNode removeElements(ListNode head, int val) {ListNode dummyHead = new ListNode(0,head);ListNode p = dummyHead;while(p.next != null){if(p.next.val == val){p.next = p.next.next;}else{p = p.next;}}return dummyHead.next;}
}

LeetCode707 设计链表

题目链接:https://leetcode.cn/problems/design-linked-list/description/

文章讲解:https://programmercarl.com/0707.设计链表.html

视频讲解:https://www.bilibili.com/video/BV1FU4y1X7WD/?vd_source=b989f2b109eb3b17e8178154a7de7a51

​ 做这种涉及到指针的题目,要时刻明确当前状态下指针到了什么位置,注意细节和合法范围条件,小心在一些细小的地方出错(比如>和>=)

image-20251121185130004

class MyLinkedList {class Node{int val;Node next;public Node(){}public Node(int val, Node next){this.val = val;this.next = next;}}Node dummyHead;int size;public MyLinkedList() {size = 0;dummyHead = new Node(0, null);}public int get(int index) {if(index < 0 || index >= size){return -1;}Node cur = dummyHead;for(int i = 0;i <= index;i++){cur = cur.next;}return cur.val;}public void addAtHead(int val) {Node newNode = new Node(val, dummyHead.next);dummyHead.next = newNode;size++;}public void addAtTail(int val) {Node cur = dummyHead;while(cur.next != null){cur = cur.next;}Node newNode = new Node(val, null);cur.next = newNode;size++;}public void addAtIndex(int index, int val) {if(index < 0 || index > size){return;}Node cur = dummyHead;Node pre = null;for(int i = 0;i <= index;i++){pre = cur;cur = cur.next;}Node newNode = new Node(val, cur);pre.next = newNode;size++;}public void deleteAtIndex(int index) {if(index < 0 || index >= size){return;}Node cur = dummyHead;Node pre = null;for(int i = 0;i <= index;i++){pre = cur;cur = cur.next;}pre.next = cur.next;size--;}
}

LeetCode206 反转链表

题目链接:https://leetcode.cn/problems/reverse-linked-list/description/

文章讲解:https://programmercarl.com/0206.翻转链表.html

视频讲解:https://www.bilibili.com/video/BV1nB4y1i7eL/?vd_source=b989f2b109eb3b17e8178154a7de7a51

​ 采用双指针的思路,cur指向当前节点,pre指向上一个节点,temp指向下一个节点,仔细画画图分析遍历过程,还是很清晰的

image-20251121171812263

class Solution {public ListNode reverseList(ListNode head) {ListNode cur = head;ListNode pre = null;while(cur != null){ListNode temp = cur.next;cur.next = pre;pre = cur;cur = temp;}return pre;}
}

​ 依照双指针解法的思路,还可以使用递归解决此问题:

image-20251121172636025

class Solution {public ListNode reverse(ListNode cur, ListNode pre){if(cur == null){return pre;}ListNode temp = cur.next;cur.next = pre;return reverse(temp, cur);}public ListNode reverseList(ListNode head) {return reverse(head, null);}
}
http://www.rkmt.cn/news/56652.html

相关文章:

  • 2025年11月北京/东城区/西城区/朝阳区/海淀区/丰台区/石景山区遗产继承律师,遗产咨询律所Top10专业推荐排行权威榜单
  • hspice 写了一个振幅可变的电压源,并且可以进行直流偏执
  • 实用指南:【设计模式】适配器模式(Adapter)
  • FPGA专用CLKUSR时钟引脚严重警告——Cyclone 10 GX
  • rich dataset 3D人体场景数据集
  • ICPC2025沈阳打铁日志
  • 实用指南:一文搞懂 DeepSeek API:兼容 OpenAI 接口的智能对话模型调用指南
  • 形容词Test
  • 最佳加密货币钱包选择指南:企业级安全与功能全解析
  • 留学中介文书原创占比谁最大?业内锐评的专业榜单来了!
  • Windows 7 Windows Server 2008 R2 简体中文版下载 (2025 年 11 月更新)
  • AI元人文:不完美中的前行——挑战回应、技术路径与治理框架
  • 2025留学中介权威发布:专业文书实力与稳定服务双重保障
  • 产品焕新升级,解锁高效管理新姿势 - RAIN
  • 留学中介大揭秘!十大留学机构排名TOP10助你高效申请
  • 上海除甲醛公司十大品牌全景解析:凡斯环保领衔,打造健康室内环境
  • P1387 最大正方形
  • markdown的初级使用教程
  • 腾讯云服务器手动安装 Docker 记录:好记性不如烂笔头
  • 留学中介排行榜TOP10怎么选:看哪家申请更强
  • 浙江初中生数学提分老师怎么选?适配杭州、宁波、绍兴、温州学生
  • 2025留学咨询机构实力榜 TOP10专业团队文本输出强强对决
  • 留学咨询效率哪家高?2025十大留学机构榜单推荐
  • 2025 最新制造业系统服务商权威推荐榜:国际协会测评认证 + 5000 + 政企案例背书,制造业生产管理系统/制造业进销存系统/制造业 MES 系统/制造业工单系统服务商推荐
  • 易路人才薪酬专家Agent赋能全球知名智能家居品牌:AI驱动的薪酬管理新范式
  • 超越生产:质量关口的“前移”与田口方法
  • 2025佛山出口退税公司排名与推荐:36个月申报期不踩坑,这样选机构退税快又稳
  • 南通一对一家教机构推荐:2026南通最新课外辅导口碑排行榜
  • 【重磅】2025两院院士增选揭晓:144位顶尖专家入选,AI、芯片与量子技术成最大赢家!
  • nc测试连接稳定性