当前位置:网站首页>【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;//返回整个链表
}
}
边栏推荐
- ThinkPHP advanced
- ATSS:自动选择样本,消除Anchor based和Anchor free物体检测方法之间的差别
- process. env. NODE_ ENV
- 自动、智能、可视!深信服SSLO方案背后的八大设计
- Trace the source of drugs and tamp the safety dike
- Microservice tracking SQL (support Gorm query tracking under isto control)
- The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple
- Detailed explanation of stm32adc analog / digital conversion
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
- Zhang Chi's class: several types and differences of Six Sigma data
猜你喜欢

HR interview: the most common interview questions and technical answers

2023届春招实习-个人面试过程和面经分享

Detailed explanation of stm32adc analog / digital conversion

TensorFlow團隊:我們沒被拋弃

三星率先投产3nm芯片,上海应届硕士生可直接落户,南开成立芯片科学中心,今日更多大新闻在此...
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)

【php毕业设计】基于php+mysql+apache的教材管理系统设计与实现(毕业论文+程序源码)——教材管理系统

Introduction to RT thread env tool (learning notes)

Microservice tracking SQL (support Gorm query tracking under isto control)

一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图...
随机推荐
2023届春招实习-个人面试过程和面经分享
Telecommuting experience? Let's introduce ourselves ~ | community essay solicitation
软件测试的可持续发展,必须要学会敲代码?
电脑屏幕变色了怎么调回来,电脑屏幕颜色怎么改
ABAP-调用Restful API
laravel的模型删除后动作
Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
VIM from dislike to dependence (22) -- automatic completion
What is the forkjoin framework in the concurrent programming series?
一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图...
分享在大疆DJI(深圳总部)工作的日常和福利
大龄测试/开发程序员该何去何从?是否会被时代抛弃?
Pico,是要拯救还是带偏消费级VR?
STM32ADC模拟/数字转换详解
MySQL advanced 4
Logical analysis of automatic decision of SAP CRM organization model
Pocket Network为Moonbeam和Moonriver RPC层提供支持
从 MLPerf 谈起:如何引领 AI 加速器的下一波浪潮
2022 Moonriver global hacker song winning project list
Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment