news 2026/5/8 2:38:23

Leetcode3

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Leetcode3

Leetcode3

  • 203.移除链表元素
  • 707.设计链表
  • 206.反转链表

203.移除链表元素

Java

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */classSolution{publicListNoderemoveElements(ListNodehead,intval){ListNodenode0=newListNode(-1,head);ListNodecurrent=node0;while(current.next!=null){if(current.next.val==val){current.next=current.next.next;}else{current=current.next;}}returnnode0.next;}}

用在链表中head前面添加一个节点node0

707.设计链表

classMyLinkedList{classLinkNode{intval;LinkNodenext;LinkNode(intval){this.val=val;}}intsize;LinkNodehead;publicMyLinkedList(){this.size=0;this.head=newLinkNode(0);}publicintget(intindex){LinkNodecurrent=head;for(inti=0;i<=index;i++){if(current.next==null){return-1;}current=current.next;}returncurrent.val;}publicvoidaddAtHead(intval){size++;LinkNodenode0=newLinkNode(val);node0.next=head.next;head.next=node0;}publicvoidaddAtTail(intval){LinkNodeendNode=newLinkNode(val);LinkNodecurrent=head;while(current.next!=null){current=current.next;}current.next=endNode;size++;}publicvoidaddAtIndex(intindex,intval){if(index<0||index>size){return;}LinkNodenode0=newLinkNode(val);LinkNodecurrent=head;for(inti=0;i<index;i++){current=current.next;}node0.next=current.next;current.next=node0;size++;}publicvoiddeleteAtIndex(intindex){if(index<0||index>=size){return;}LinkNodecurrent=head;for(inti=0;i<index;i++){current=current.next;}current.next=current.next.next;size--;}}/** * Your MyLinkedList object will be instantiated and called as such: * MyLinkedList obj = new MyLinkedList(); * int param_1 = obj.get(index); * obj.addAtHead(val); * obj.addAtTail(val); * obj.addAtIndex(index,val); * obj.deleteAtIndex(index); */

206.反转链表

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */classSolution{publicListNodereverseList(ListNodehead){ListNodecurrent=head;ListNodepre=null;ListNodetemp0=null;while(current!=null){temp0=current.next;current.next=pre;pre=current;current=temp0;}returnpre;}}

temp0 = current.next;
将下一个节提前保存,因为后面current.next指向了前面的节点。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/1 0:32:16

Godot资源解包技术深度解析:从PCK文件结构到自动化处理流程

Godot资源解包技术深度解析&#xff1a;从PCK文件结构到自动化处理流程 【免费下载链接】godot-unpacker godot .pck unpacker 项目地址: https://gitcode.com/gh_mirrors/go/godot-unpacker 本文深入探讨Godot引擎资源打包格式的技术原理&#xff0c;详细解析PCK文件的…

作者头像 李华
网站建设 2026/5/1 6:18:01

ROS2概念之分布式通信

智能机器人的功能繁多&#xff0c;全都放在一个计算机里&#xff0c;经常会遇到计算能力不够、处理出现卡顿等情况&#xff0c;如果可以将这些任务拆解&#xff0c;分配到多个计算机中运行岂不是可以减轻压力&#xff1f; 这就是分布式系统&#xff0c;可以实现多计算平台上的任…

作者头像 李华
网站建设 2026/4/20 18:20:02

LobeChat Bing搜索引擎优化

LobeChat 与 Bing 搜索引擎集成的技术实践 在今天&#xff0c;构建一个真正智能的对话系统早已不再只是“调用大模型 API”这么简单。用户期望的是能理解上下文、具备实时信息获取能力、并且可以无缝对接业务场景的 AI 助手。然而&#xff0c;大多数开源聊天界面仍停留在基础交…

作者头像 李华
网站建设 2026/4/29 21:02:50

终极WPS文档在线预览指南:快速集成完整教程

终极WPS文档在线预览指南&#xff1a;快速集成完整教程 【免费下载链接】wps-view-vue wps在线编辑、预览前端vue项目&#xff0c;基于es6 项目地址: https://gitcode.com/gh_mirrors/wp/wps-view-vue 在当今数字化办公环境中&#xff0c;文档在线预览已成为提升工作效率…

作者头像 李华
网站建设 2026/4/25 8:06:02

绝区零自动化脚本开发:10分钟快速上手指南

绝区零自动化脚本开发&#xff1a;10分钟快速上手指南 【免费下载链接】ZenlessZoneZero-OneDragon 绝区零 一条龙 | 全自动 | 自动闪避 | 自动每日 | 自动空洞 | 支持手柄 项目地址: https://gitcode.com/gh_mirrors/ze/ZenlessZoneZero-OneDragon 在游戏开发领域&…

作者头像 李华