news 2026/7/7 20:11:55

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/7/7 19:44:28

iOS激活锁终极绕过:5步解锁完整方案

iOS激活锁终极绕过&#xff1a;5步解锁完整方案 【免费下载链接】applera1n icloud bypass for ios 15-16 项目地址: https://gitcode.com/gh_mirrors/ap/applera1n 当你的iPhone因为忘记Apple ID密码或购买二手设备而陷入激活锁困境时&#xff0c;AppleRa1n工具提供了一…

作者头像 李华
网站建设 2026/7/7 17:28:36

内容访问辅助工具:优化阅读体验的方法指南

还在为内容访问限制而烦恼吗&#xff1f;想要更顺畅地阅读优质内容却总是遇到阻碍&#xff1f;今天我要为你介绍一款实用的内容访问辅助工具&#xff0c;它能帮你优化阅读体验&#xff0c;让你更高效地获取知识&#xff01;&#x1f31f; 【免费下载链接】bypass-paywalls-chro…

作者头像 李华
网站建设 2026/7/7 4:25:22

LobeChat性能监控工具推荐:Prometheus + Grafana整合方案

LobeChat性能监控工具推荐&#xff1a;Prometheus Grafana整合方案 在今天的企业AI应用浪潮中&#xff0c;LobeChat 这类基于大语言模型&#xff08;LLM&#xff09;的智能对话系统早已不再是“能跑就行”的实验项目。越来越多团队将其部署为生产级服务——用于客服自动化、内…

作者头像 李华
网站建设 2026/7/7 13:26:47

基于SpringBoot+Vue的果蔬作物疾病防治系统管理系统设计与实现【Java+MySQL+MyBatis完整源码】

摘要 随着现代农业的快速发展&#xff0c;果蔬作物的疾病防治成为影响农产品质量和产量的关键因素。传统的防治方式依赖人工经验&#xff0c;效率低下且难以实现精准化管理。信息技术的发展为农业智能化提供了新的解决方案&#xff0c;通过构建数字化管理系统&#xff0c;可以有…

作者头像 李华
网站建设 2026/7/7 18:42:04

LobeChat代理商培训资料自动生成

LobeChat 代理商培训资料自动生成 在企业加速拥抱 AI 的今天&#xff0c;一个现实问题日益凸显&#xff1a;我们手握强大的大语言模型&#xff0c;却依然难以向客户交付真正可用的智能助手。很多团队能跑通 Llama 或 Qwen&#xff0c;但最终给客户的还是个命令行界面——这显然…

作者头像 李华