当前位置:网站首页>LeetCode 19. 删除链表的倒数第 N 个结点
LeetCode 19. 删除链表的倒数第 N 个结点
2022-06-26 04:56:00 【碳烤小肥羊。。。】
题目描述:给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
示例 1:
输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
示例 2:
输入:head = [1], n = 1
输出:[]
示例 3:
输入:head = [1,2], n = 1
输出:[1]
思路解析:
指针的经典应用,如果要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点就可以了。
首先这里我推荐大家使用虚拟头结点,这样方便处理删除实际头结点的逻辑。
定义fast指针和slow指针,初始值为虚拟头结点,如图:

fast首先走n + 1步 ,为什么是n+1呢,因为只有这样同时移动的时候slow才能指向删除节点的上一个节点(方便做删除操作),如图:

fast和slow同时移动,直到fast指向末尾,如图:

删除slow指向的下一个节点,如图:

代码实现方式一:
public static ListNode removeNthFromEnd(ListNode head, int n) {
// 创建一个虚拟头结点
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
// 定义快慢指针
ListNode fast = dummyNode, slow = dummyNode;
// fast先走 n+1 步,这样slow就指向待删除结点的上一个结点
while (n >= 0){
fast = fast.next;
n--;
}
// fast,slow同时向后移送
while (fast != null){
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummyNode.next;
}
代码实现方式二:
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode slow = dummy;
ListNode fast = dummy;
while (n > 0) {
fast = fast.next;
n--;
}
// 待删除节点slow 的上一节点
ListNode prev = null;
while (fast != null) {
prev = slow;
slow = slow.next;
fast = fast.next;
}
// 上一节点的next指针绕过 待删除节点slow 直接指向slow的下一节点
prev.next = slow.next;
return dummy.next;
}
边栏推荐
- 为什么许多shopify独立站卖家都在用聊天机器人?一分钟读懂行业秘密!
- Sort query
- torchvision_ Transform (image enhancement)
- 2022.2.15
- 202.2.9
- Multipass Chinese document - remote use of multipass
- Resample
- 1.14 learning summary
- Why do many Shopify independent station sellers use chat robots? Read industry secrets in one minute!
- Use fill and fill in Matplotlib_ Between fill the blank area between functions
猜你喜欢

YOLOV5超参数设置与数据增强解析

Genius makers: lone Rangers, technology giants and AI | ten years of the rise of in-depth learning
![[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching](/img/e4/27611abdd000019e70f4447265808c.jpg)
[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching

1.24 learning summary

Zuul 实现动态路由

Install Damon database

Final review of brain and cognitive science

Machine learning final exercises

2. < tag dynamic programming and conventional problems > lt.343 integer partition

86.(cesium篇)cesium叠加面接收阴影效果(gltf模型)
随机推荐
-Discrete Mathematics - Analysis of final exercises
numpy 随机数
1.20 learning summary
2022.2.16
Wechat applet exits the applet (navigator and api--wx.exitminiprogram)
Image translation /gan:unsupervised image-to-image translation with self attention networks
Some parameter settings and feature graph visualization of yolov5-6.0
Motivational skills for achieving goals
0622-马棕榈跌9%
钟珊珊:被爆锤后的工程师会起飞|OneFlow U
Créateur de génie: cavalier solitaire, magnat de la technologie et ai | dix ans d'apprentissage profond
Multipass Chinese documents - improve mount performance
[IDE(ImageBed)]Picgo+Typora+aliyunOSS部署博客图床(2022.6)
2022.2.11
Modify the case of the string title(), upper(), lower()
File upload and security dog
图像翻译/GAN:Unsupervised Image-to-Image Translation with Self-Attention Networks基于自我注意网络的无监督图像到图像的翻译
2022.2.10
Zuul 实现动态路由
Simple application of KMP