当前位置:网站首页>[record of question brushing] 19. Delete the penultimate node of the linked list
[record of question brushing] 19. Delete the penultimate node of the linked list
2022-07-23 14:49:00 【InfoQ】
One 、 Title Description
n
Input : head = [1,2,3,4,5], n = 2
Output : [1,2,3,5]
Input :head = [1], n = 1
Output :[]
Input :head = [1,2], n = 1
Output :[1]
- 1 <= sz <= 30
- 0 <= Node.val <= 100
- 1 <= n <= szAdvanced :Can you try a scan implementation ?
II. Train of thought analysis
- Let's make this window a fixed length
nA window of ,
- The left side of the window is the starting point of the linked list .
- Then move the window , When the right side of the window moves to the end node of the linked list, it is empty , The left side of the window points to the one to be deleted Last but not least
nNodes

3、 ... and 、 Code implementation
/**
* 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;
}
}
Complexity analysis
- Time complexity :, among
n It's the length of the list
- Spatial complexity :
Running results

summary
Double pointer + Windows are similar concepts ( Fixed length )n边栏推荐
- [paper notes] mobile robot navigation method based on hierarchical depth reinforcement learning
- Quanzhi f1c100s/f1c200s learning notes (13) -- lvgl transplantation
- 【 langage c】 devinez jeux numériques + applet d'arrêt
- 104 maximum depth of binary tree and 543 diameter of binary tree and 124 maximum path sum of binary tree
- 【刷题记录】19. 删除链表的倒数第 N 个结点
- Official wechat product! Applet automation framework minium sharing Preview
- Program design of dot matrix Chinese character display of basic 51 single chip microcomputer
- Opencv calculation outsourcing rectangle
- 运维高级作业03
- Authing 支持 Zadig 啦!云原生用户统一认证快速对接
猜你喜欢

【C语言】猜数字小游戏+关机小程序

Sword finger offer 46. translate numbers into strings

【刷题记录】19. 删除链表的倒数第 N 个结点

R语言实战应用案例:绘图篇(三)-多种组合图型绘制

cmake笔记

【WinForm】关于截图识别数字并计算的桌面程序实现方案
![[test platform development] 23. interface assertion function - save interface assertion and edit echo](/img/36/aed4feb6a4e40b6b5c06206a8ed440.png)
[test platform development] 23. interface assertion function - save interface assertion and edit echo

【测试平台开发】二十、完成编辑页发送接口请求功能

运维高级作业03

Typora图床配置详细教程
随机推荐
Program design of dot matrix Chinese character display of basic 51 single chip microcomputer
微信官方出品!小程序自动化框架 minium 分享预告
运维高级作业03
LZ77文件压缩
mysql函数汇总之字符串函数
【软件测试】如何梳理你测试的业务
Cool code rain dynamic background registration page
对象使用过程中背后调用了哪些方法
[WinForm] desktop program implementation scheme for screenshot recognition and calculation
Qu'est - ce que le codage par titre?
(heavy chain dissection) Magic Tree
【我可以做你的第一个项目吗?】GZIP的细节简介和模拟实现
Is it risky and safe to open a mobile stock account?
正则表达式常用语法解析
Design and implementation of websocket universal packaging
利用shell脚本实现封禁扫描频率过高的ip
【小程序自动化Minium】三、元素定位- WXSS 选择器的使用
cmake笔记
Which is a good fixed asset management system? What are the fixed asset management platforms?
OpenCV计算外包矩形