当前位置:网站首页>【刷题记录】19. 删除链表的倒数第 N 个结点
【刷题记录】19. 删除链表的倒数第 N 个结点
2022-07-23 09:20:00 【InfoQ】
一、题目描述
n
输入: head = [1,2,3,4,5], n = 2
输出: [1,2,3,5]
输入:head = [1], n = 1
输出:[]
输入:head = [1,2], n = 1
输出:[1]
- 1 <= sz <= 30
- 0 <= Node.val <= 100
- 1 <= n <= sz进阶:你能尝试使用一趟扫描实现吗?
二丶思路分析
- 这个窗口我们让它是定长
n的一个窗口,
- 窗口左边为链表的起点。
- 然后移动这个窗口,当窗口右边移动到链表的末端节点时候为空时候,窗口的左边指向的即是要被删除的 倒数第
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, head);
ListNode R = head;
ListNode L = dummy;
for (int i = 0; i < n; ++i) {
R = R.next;
}
while (R != null) {
R = R.next;
L = L.next;
}
L.next = L.next.next;
ListNode res = dummy.next;
return res;
}
}
复杂度分析
- 时间复杂度:,其中
n 是链表的长度
- 空间复杂度:
运行结果

总结
双指针 + 窗口都类似概念(定长的)n边栏推荐
- APtos 简介及机制
- Is it risky and safe to open a mobile stock account?
- Offline data interoperability
- C language project practice: 24 point game calculator (based on knowledge points such as structure, pointer, function, array, loop, etc.)
- After using vscode to format the code, save and find that the code is messy again. What should I do? Vs remove formatting
- The win11 installation system prompts that VirtualBox is incompatible and needs to uninstall the solution of virtual, but the uninstall list cannot find the solution of virtual
- 【WinForm】关于截图识别数字并计算的桌面程序实现方案
- 云呐-如何加强固定资产管理?怎么加强固定资产管理?
- js软件卸载提示表情跟随鼠标变化js特效
- Yunna | how to manage fixed assets? How to manage fixed assets?
猜你喜欢

对象使用过程中背后调用了哪些方法

Canvas 从入门到劝朋友放弃(图解版)

Cool code rain dynamic background registration page

10 years of software testing engineer experience, very confused

R language practical application case: drawing part (III) - drawing of multiple combination patterns

C语言实现课堂随机点名系统

右键新建txt,新建文本文件不见了,通过添加注册表就可以解决,找来找去办法解决不了的终极办法
![[applet automation minium] III. element positioning - use of wxss selector](/img/ec/51eadd08bea18f8292aa3521f11e8a.png)
[applet automation minium] III. element positioning - use of wxss selector

【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID

Yunna - how to strengthen fixed asset management? How to strengthen the management of fixed assets?
随机推荐
扁平样式反馈表单页面
【PyQt5安装以及使用】
【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID
js日历样式饼图统计插件
[applet automation minium] i. framework introduction and environment construction
C语言实现课堂随机点名系统
Tell you Web3.0 I understand
FFmpeg 1 - 概览/安装
js纹理样式饼图插件
[pyqt5 installation and use]
R language practical application case: drawing part (III) - drawing of multiple combination patterns
Okrk3399 Development Board Reserved i2c4 Mounting EEPROM
[C language] number guessing game + shutdown applet
Aruba learning notes 05 configuration architecture WLAN configuration architecture
Sword finger offer 46. translate numbers into strings
Flat style feedback form page
云呐|怎样管理固定资产?如何进行固定资产管理?
可以进行2D或3D三角剖分的一些库
Surrounded Regions
利用js自动解析执行xss