当前位置:网站首页>【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;//返回整个链表
}
}
边栏推荐
- Preorder, inorder, follow-up of binary tree (non recursive version)
- 开机时小键盘灯不亮的解决方案
- 三星率先投产3nm芯片,上海应届硕士生可直接落户,南开成立芯片科学中心,今日更多大新闻在此...
- ADS算力芯片的多模型架构研究
- Summer Challenge harmonyos canvas realize clock
- 从 MLPerf 谈起:如何引领 AI 加速器的下一波浪潮
- laravel的模型删除后动作
- picgo快捷键 绝了这人和我的想法 一模一样
- Please, stop painting star! This has nothing to do with patriotism!
- 揭秘慕思“智商税”:狂砸40亿搞营销,发明专利仅7项
猜你喜欢
Nuxt.js数据预取
Tensorflow team: we haven't been abandoned
Deep operator overloading (2)
What is the forkjoin framework in the concurrent programming series?
[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)
2022 Moonriver全球黑客松优胜项目名单
ATSs: automatically select samples to eliminate the difference between anchor based and anchor free object detection methods
AVL 平衡二叉搜索树
Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme
Detailed explanation of stm32adc analog / digital conversion
随机推荐
Task. Run(), Task. Factory. Analysis of behavior inconsistency between startnew() and new task()
基于PHP的轻量企业销售管理系统
搜索框和按钮缩放时会有缝隙的bug
软件测试的可持续发展,必须要学会敲代码?
【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)
Preorder, inorder, follow-up of binary tree (non recursive version)
[200 opencv routines] 216 Draw polylines and polygons
Solution to the problem that the keypad light does not light up when starting up
There is a difference between u-standard contract and currency standard contract. Will u-standard contract explode
idea启动Command line is too long问题处理
Nuxt.js数据预取
Seate中用了shardingjdbc 就不能用全局事务了吗?
Rhcsa fourth day operation
电脑屏幕变色了怎么调回来,电脑屏幕颜色怎么改
Factory high-precision positioning management system, digital safety production management
Summer Challenge harmonyos canvas realize clock
What is the forkjoin framework in the concurrent programming series?
Smart Party Building: faith through time and space | 7.1 dedication
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
Day 3 of rhcsa study