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

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指向了前面的节点。

http://www.gsyq.cn/news/113124.html

相关文章:

  • LobeChat重要公告置顶策略
  • LaTeX公式转换Word完整教程:3分钟掌握高效学术写作技巧
  • 文泉驿微米黑字体极速部署手册
  • 网盘下载限速终结者:一键获取真实直链的完整攻略
  • 智谱AI GLM系列模型与LobeChat完美融合方案
  • Balena Etcher镜像烧录工具终极指南
  • RTL8852BE Linux驱动:完美解决无线网卡识别难题的完整方案
  • 解决OpenCore Legacy Patcher网络连接故障的完整指南
  • DeepSeek-Math:7B参数数学AI模型的5大核心技术突破
  • 【毕业设计】SpringBoot+Vue+MySQL 工作量统计系统平台源码+数据库+论文+部署文档
  • Java Web 果蔬作物疾病防治系统系统源码-SpringBoot2+Vue3+MyBatis-Plus+MySQL8.0【含文档】
  • LobeChat版本更新日志生成
  • 小米运动自动刷步数工具:智能管理你的健康数据
  • LobeChat蓝绿部署实践:确保服务不间断
  • 阿里验证码Web和H5客户端V3架构接入鼠标拖拽滑块离开对话框释放鼠标时一直处于验证中问题
  • LobeChat单元测试用例生成实验
  • LobeChat体育赛事即时点评
  • 如何快速修复Windows运行库问题:Visual C++ Redistributable终极指南
  • 高校危化试剂仓储系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
  • 基于SpringBoot+Vue的高校物品捐赠管理系统管理系统设计与实现【Java+MySQL+MyBatis完整源码】
  • LobeChat能否集成股票行情?金融数据分析助手开发
  • Ofd2Pdf使用教程:从OFD到PDF的快速转换指南
  • 终极PDF对比指南:用diff-pdf轻松识别文档差异
  • LobeChat与Redis缓存结合提升并发处理能力
  • 1、量子漫步与搜索算法:从理论到实践
  • 3、量子漫步与测量过程入门
  • 微信网页版访问困境破局:wechat-need-web插件实战指南
  • 6、格罗弗算法及其推广详解
  • VS Code内置终端调用LobeChat的实验性功能
  • LobeChat OCR插件开发设想:让AI看懂图片中的文字