当前位置:网站首页>【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;//返回整个链表
}
}
边栏推荐
- What is the forkjoin framework in the concurrent programming series?
- Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
- process. env. NODE_ ENV
- Smart Party Building: faith through time and space | 7.1 dedication
- Embedded development: five revision control best practices
- Zhang Chi's class: several types and differences of Six Sigma data
- How does win11 set user permissions? Win11 method of setting user permissions
- Stm32f4-tft-spi timing logic analyzer commissioning record
- Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme
- Preorder, inorder, follow-up of binary tree (non recursive version)
猜你喜欢

Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12

Reading notes of top performance version 2 (V) -- file system monitoring

Zero copy technology of MySQL

2022 Moonriver全球黑客松优胜项目名单

近半年内连获5家“巨头”投资,这家智能驾驶“黑马”受资本追捧

Deep operator overloading (2)

process.env.NODE_ENV

揭秘慕思“智商税”:狂砸40亿搞营销,发明专利仅7项

Tensorflow team: we haven't been abandoned

How to adjust the size of computer photos to what you want
随机推荐
process. env. NODE_ ENV
Seata中1.5.1 是否支持mysql8?
七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)
Redis秒杀demo
Nuxt.js数据预取
Create employee data in SAP s/4hana by importing CSV
嵌入式开发:5个修订控制最佳实践
How does win11 set user permissions? Win11 method of setting user permissions
华为发布HCSP-Solution-5G Security人才认证,助力5G安全人才生态建设
DO280管理应用部署--pod调度控制
【显存优化】深度学习显存优化方法
综述 | 激光与视觉融合SLAM
Win11如何设置用户权限?Win11设置用户权限的方法
Factory high-precision positioning management system, digital safety production management
ThinkPHP进阶
从 MLPerf 谈起:如何引领 AI 加速器的下一波浪潮
【php毕业设计】基于php+mysql+apache的教材管理系统设计与实现(毕业论文+程序源码)——教材管理系统
RT-Thread Env 工具介绍(学习笔记)
[open source data] open source data set for cross modal (MRI, Meg, eye movement) human spatial memory research based on virtual reality scenes
微服务追踪SQL(支持Isto管控下的gorm查询追踪)