news 2026/4/23 17:55:35

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/4/23 12:34:08

LobeChat重要公告置顶策略

LobeChat&#xff1a;重塑AI交互入口的开源实践 在今天&#xff0c;几乎每个开发者都接触过大语言模型——无论是用GPT写一段代码&#xff0c;还是让Claude帮忙润色邮件。但你有没有想过&#xff0c;为什么我们总是在“聊天框”里和这些强大的AI对话&#xff1f;这个看似简单的…

作者头像 李华
网站建设 2026/4/23 0:55:44

抖音内容保存革命:告别录屏时代的高效下载方案

抖音内容保存革命&#xff1a;告别录屏时代的高效下载方案 【免费下载链接】douyin-downloader 项目地址: https://gitcode.com/GitHub_Trending/do/douyin-downloader 还记得那个深夜&#xff0c;你心仪的主播正在抖音进行一场精彩绝伦的直播&#xff0c;你手忙脚乱地…

作者头像 李华
网站建设 2026/4/23 11:13:24

LobeChat缓存穿透预防方案

LobeChat 缓存穿透预防方案 在构建现代 AI 聊天应用时&#xff0c;性能与安全的平衡往往比我们想象中更脆弱。一个看似简单的“获取会话”请求&#xff0c;若被恶意利用&#xff0c;可能在几分钟内拖垮整个后端服务——这正是 缓存穿透 的真实威胁。 LobeChat 作为基于 Next.js…

作者头像 李华
网站建设 2026/4/23 12:47:24

LobeChat百家号内容运营方案

LobeChat 技术架构与应用实践深度解析 在大语言模型&#xff08;LLM&#xff09;技术席卷全球的今天&#xff0c;AI 聊天机器人早已不再是实验室里的概念玩具。从智能客服到个人助手&#xff0c;再到企业知识管理&#xff0c;各类场景中都能看到它的身影。然而&#xff0c;当人…

作者头像 李华
网站建设 2026/4/19 17:41:38

文泉驿微米黑字体极速部署手册

文泉驿微米黑字体极速部署手册 【免费下载链接】fonts-wqy-microhei Debian package for WenQuanYi Micro Hei (mirror of https://anonscm.debian.org/git/pkg-fonts/fonts-wqy-microhei.git) 项目地址: https://gitcode.com/gh_mirrors/fo/fonts-wqy-microhei &#x…

作者头像 李华
网站建设 2026/4/23 12:48:04

5步终极指南:iPhone秒速AI绘图全流程解密

你是否想过在手机上运行专业级AI绘图模型&#xff1f;当别人还在等待云端服务响应时&#xff0c;你已经能在iPhone上3秒生成高质量图像。本文将彻底解析移动端diffusion模型部署的完整技术链&#xff0c;让你掌握从模型压缩到CoreML转换的核心方法。 【免费下载链接】denoising…

作者头像 李华