当前位置:网站首页>【刷题记录】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边栏推荐
- R语言实战应用案例:绘图篇(三)-多种组合图型绘制
- Dynamic programming -- knapsack problem
- 【C語言】猜數字小遊戲+關機小程序
- Several hole filling methods of point cloud (mesh) (1)
- 4. Find the median of two positive arrays
- Yunna - how to strengthen fixed asset management? How to strengthen the management of fixed assets?
- 云呐|怎样管理固定资产?如何进行固定资产管理?
- js软件卸载提示表情跟随鼠标变化js特效
- Chapter 3 complex query
- Optimize Huawei ECs to use key login
猜你喜欢

Yunna - how to strengthen fixed asset management? How to strengthen the management of fixed assets?

AI acceleration gesture recognition experience based on efr32mg24

About flex layout justify content: the last solution to the misalignment of space around and why it is solved like this is a discussion

Surrounded Regions

关于flex布局justify-content:space-around最后一个不对齐的解决方法和为什么这样子解决是讨论

Vk36n5d anti power interference / mobile phone interference 5-key 5-channel touch detection chip anti freeze function ponding in the touch area can still be operated

Program design of dot matrix Chinese character display of basic 51 single chip microcomputer

C语言项目实战:24点游戏计算器(基于结构体、指针、函数、数组、循环等知识点)

利用shell脚本实现封禁扫描频率过高的ip

JS calendar style pie chart statistics plug-in
随机推荐
Aruba learning notes 05 configuration architecture WLAN configuration architecture
什么是Per-Title编码?
因为资源限制,导致namenode启动失败,报错unable to create new native thread
Quick introduction to PKI system
R language practical application case: drawing part (III) - drawing of multiple combination patterns
Using shell script to block IP with high scanning frequency
ValidationError: Invalid options object. Dev Server has been initialized using an options object th
【测试平台开发】23. 接口断言功能-保存接口断言和编辑回显
Is it risky and safe to open a mobile stock account?
【测试平台开发】十七、接口编辑页面实现下拉级联选择,绑定接口所属模块...
OKRK3399開發板預留I2C4掛載EEPROM
对象使用过程中背后调用了哪些方法
Experience in developing large crawlers in requests Library
利用shell脚本实现封禁扫描频率过高的ip
建议思源笔记能够兼容第三方同步盘
Using JS to parse and execute XSS automatically
云呐|公司固定资产如何管理?公司固定资产如何管理比较好?
Towhee 每周模型
Cool code rain dynamic background registration page
【 langage c】 devinez jeux numériques + applet d'arrêt