当前位置: 首页 > 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.cnnetsun.cn/news/97100.html

相关文章:

  • LobeChat指标监控告警设置
  • LobeChat Hugging Face Model Hub直连:一键拉取热门模型
  • XiaoMusic:小爱音箱音乐播放的终极解决方案
  • LobeChat参会凭证短信内容
  • LobeChat活动报名确认邮件
  • LobeChat能否支持暗物质探测?宇宙未解之谜推理模拟器
  • LobeChat悼念辞撰写辅助工具
  • FreeMove神器:一键解决Windows系统盘空间危机
  • LobeChat二次验证说明文字
  • OneMore插件终极指南:如何快速实现OneNote页面目录自动置顶
  • OneMore插件终极指南:如何快速创建自动置顶的页面目录容器
  • SpringBoot+Vue 高校宣讲会管理系统平台完整项目源码+SQL脚本+接口文档【Java Web毕设】
  • Java SpringBoot+Vue3+MyBatis 供应商管理系统系统源码|前后端分离+MySQL数据库
  • Ofd2Pdf使用教程:从OFD到PDF的快速转换指南
  • 从信息到意义——为什么说整合信息论是一种关于意义的理论
  • 避免空白字符的To-Do应用开发
  • 超越-env-一份成熟的应用程序配置指南
  • 一文搞懂爬山算法!!!
  • 【金猿产品展】纷享销客——用智能科技和行业智慧赋能企业增长
  • Motrix浏览器扩展:重新定义你的下载体验
  • 20、量子计算中的线性代数基础
  • LobeChat知识点讲解生成器开发
  • asio的socket创建与连接的基础实现和与C风格的socket网络通信的对比
  • Locale Emulator终极指南:系统区域模拟与多语言软件解决方案
  • LobeChat数据库存储机制解析:对话记录保存在哪里?
  • Obsidian主题配置终极指南:轻松打造个性化知识管理界面
  • OBS-VST插件终极指南:5分钟打造专业直播音效
  • LobeChat差评挽回话术建议
  • 3、量子力学的奇妙世界:从争议到多元解读
  • LobeChat国庆节爱国主题文案