当前位置:网站首页>【Hot100】19. 删除链表的倒数第 N 个结点
【Hot100】19. 删除链表的倒数第 N 个结点
2022-07-01 15:49:00 【王六六的IT日常】

虚拟头节点+双指针
fast先移动n+1步,超前n个结点,使得slow和fast之间间隔n-1个节点.
间隔n-1个节点即超前n个节点。
/** * 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; } * } */
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
//虚拟头节点
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode slow = dummy,fast = dummy;
//使slow和fast间隔n-1个节点
for(int i=0;i<=n;i++){
fast = fast.next;
}
while(fast != null){
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummy.next;//返回整个链表
}
}
边栏推荐
- 2022-07-01日报:谷歌新研究:Minerva,用语言模型解决定量推理问题
- For the sustainable development of software testing, we must learn to knock code?
- Deep operator overloading (2)
- The latest NLP game practice summary!
- There will be a gap bug when the search box and button are zoomed
- 七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)
- Reading notes of top performance version 2 (V) -- file system monitoring
- [target tracking] | template update time context information (updatenet) "learning the model update for Siamese trackers"
- Pico,能否拯救消费级VR?
- When ABAP screen switching, refresh the previous screen
猜你喜欢

RT-Thread Env 工具介绍(学习笔记)

What time do you get off work?!!!

自动、智能、可视!深信服SSLO方案背后的八大设计
![[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system](/img/04/11f24f12c52fb1f69e3d6f513d896b.png)
[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system

【开源数据】基于虚拟现实场景的跨模态(磁共振、脑磁图、眼动)人类空间记忆研究开源数据集

C#/VB. Net merge PDF document

三星率先投产3nm芯片,上海应届硕士生可直接落户,南开成立芯片科学中心,今日更多大新闻在此...

七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)

工厂高精度定位管理系统,数字化安全生产管理

u本位合约和币本位合约有区别,u本位合约会爆仓吗
随机推荐
ADS算力芯片的多模型架构研究
MySQL高级篇4
What is the forkjoin framework in the concurrent programming series?
Nuxt.js数据预取
Zhang Chi's class: several types and differences of Six Sigma data
从 MLPerf 谈起:如何引领 AI 加速器的下一波浪潮
Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices
Telecommuting experience? Let's introduce ourselves ~ | community essay solicitation
picgo快捷键 绝了这人和我的想法 一模一样
[每日一氵]Latex 的通讯作者怎么搞
[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)
Task. Run(), Task. Factory. Analysis of behavior inconsistency between startnew() and new task()
TensorFlow团队:我们没被抛弃
Redis high availability principle
药品溯源夯实安全大堤
Pocket network supports moonbeam and Moonriver RPC layers
求求你们,别再刷 Star 了!这跟“爱国”没关系!
What time do you get off work?!!!
[STM32 learning] w25qxx automatic judgment capacity detection based on STM32 USB storage device
Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all