news 2026/3/2 15:56:14

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/2/24 8:35:04

FontCenter终极指南:彻底告别AutoCAD字体缺失问题

FontCenter终极指南&#xff1a;彻底告别AutoCAD字体缺失问题 【免费下载链接】FontCenter AutoCAD自动管理字体插件 项目地址: https://gitcode.com/gh_mirrors/fo/FontCenter 还在为打开DWG文件时看到一堆问号而头疼吗&#xff1f;FontCenter作为一款革命性的AutoCAD字…

作者头像 李华
网站建设 2026/2/28 10:34:11

LobeChat TGI(Text Generation Inference)对接教程

LobeChat 与 TGI 对接实战&#xff1a;构建高性能私有化对话系统 在大模型应用迅速落地的今天&#xff0c;越来越多开发者不再满足于调用 OpenAI 这类公有云 API。企业关心数据安全&#xff0c;个人用户希望摆脱订阅费用&#xff0c;而所有使用者都在追求更低的响应延迟和更高的…

作者头像 李华
网站建设 2026/2/26 19:42:53

如何快速配置TPFanCtrl2:ThinkPad风扇噪音终极解决方案

如何快速配置TPFanCtrl2&#xff1a;ThinkPad风扇噪音终极解决方案 【免费下载链接】TPFanCtrl2 ThinkPad Fan Control 2 (Dual Fan) for Windows 10 and 11 项目地址: https://gitcode.com/gh_mirrors/tp/TPFanCtrl2 还在为ThinkPad笔记本的持续风扇噪音而烦恼&#xf…

作者头像 李华
网站建设 2026/2/25 13:26:24

LobeChat悬浮通知内容生成

LobeChat悬浮通知内容生成 在如今大语言模型&#xff08;LLM&#xff09;深度融入日常工作的背景下&#xff0c;用户对AI交互的期待早已超越“能用”&#xff0c;转而追求“好用”——界面直观、反馈及时、操作无感。然而&#xff0c;当我们在浏览器中与AI对话时&#xff0c;常…

作者头像 李华
网站建设 2026/2/22 15:23:54

无须付费,一键生成!

啰嗦几句 前两天推荐的那款fu昕PDF&#xff0c;后来好多小伙伴说失效了&#xff0c;我再去看时确实是鸡活不了&#xff0c;而已经&#x1f414;活成功的不受影响&#xff0c;所以大家看到这类资源的时候&#xff0c;要赶紧去注册&#xff0c;不然后续就失效了&#xff01; 言归…

作者头像 李华