当前位置: 首页 > 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.jsqmd.com/news/46958/

相关文章:

  • 2025年11月北京/东城区/西城区/朝阳区/海淀区/丰台区/石景山区遗产继承律师,遗产咨询律所Top10专业推荐排行权威榜单
  • 2025年11月北京/东城区/西城区/朝阳区/海淀区/丰台区/石景山区遗产继承、遗产纠纷,遗产咨询律师事务所权威排行榜单:专业律所推荐与选择指南
  • hspice 写了一个振幅可变的电压源,并且可以进行直流偏执
  • 南屏晚钟
  • Linux初级命令练习:通过awk、sed如何批量创建用户
  • 详细介绍:压缩与缓存调优实战指南:从0到1根治性能瓶颈(四)
  • sqli-labs 1(Less-1-Less-10)新手解题思路 - 指南
  • PyMAF 2023 单张照片估计参数化人体
  • 实用指南:【设计模式】适配器模式(Adapter)
  • 完整教程:【人工智能】神经网络的优化器optimizer(四):Adam自适应动量优化器
  • 轻松速通:TTS播放、文件播放与录音的核心功能解析!
  • 2025 中国法兰阀门十大品牌推荐:密封升级 + 场景适配,优质厂家护航流体系统安全
  • FPGA专用CLKUSR时钟引脚严重警告——Cyclone 10 GX
  • OPCUA探讨(五)——客户端代码解读:监控变量值与报警
  • 2025 年度中国截止阀十大品牌推荐:绿色智造 + 特种工况突破,引领行业高质量发展
  • 修改DTS适配遥控用户码
  • nginx性能优化之tcp调优
  • 2025年11月安徽聚乙烯瓶、高阻隔瓶、聚酯瓶、农药瓶供应商排行榜:安徽金汇龙包装领跑行业
  • 2025年11月中国/安徽/聚乙烯瓶、高阻隔瓶、聚酯瓶、农药瓶厂家TOP10推荐:安徽金汇龙包装强势登顶
  • rich dataset 3D人体场景数据集
  • ICPC2025沈阳打铁日志
  • UModel 数据治理:运维世界模型构建实践
  • 【springboot】 WebMvcConfigurer的使用
  • 2025年11月21日
  • 实用指南:一文搞懂 DeepSeek API:兼容 OpenAI 接口的智能对话模型调用指南
  • 形容词Test
  • James Watsons
  • 2025年11月江苏/徐州vr设备、vr体验馆、5d影院、9d影院、拓普互动厂家推荐榜:拓普互动强势登顶
  • 计算机网络:物理层 - 实践
  • 2025年11月中国/江苏/徐州vr设备、vr体验馆、5d影院、9d影院、拓普互动厂家TOP10:拓普互动领跑榜单