当前位置:网站首页>[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边栏推荐
- 建议思源笔记能够兼容第三方同步盘
- Optimisation du serveur Cloud Huawei avec connexion clé
- Authing 支持 Zadig 啦!云原生用户统一认证快速对接
- Wacom firmware update error 123, digital board driver cannot be updated
- Question 142 of Li Kou: circular linked list 2
- AI acceleration gesture recognition experience based on efr32mg24
- 基本51单片机点阵汉字显示程序设计
- JS calendar style pie chart statistics plug-in
- First acquaintance and search set
- [applet automation minium] i. framework introduction and environment construction
猜你喜欢

Question 142 of Li Kou: circular linked list 2

Right click to create a new TXT. The new text file is missing. You can solve it by adding a registry. Find the ultimate solution that can't be solved

Which is a good fixed asset management system? What are the fixed asset management platforms?
![[download attached] several scripts commonly used in penetration testing that are worth collecting](/img/01/3b74c5ab4168059827230578753be5.png)
[download attached] several scripts commonly used in penetration testing that are worth collecting

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

云呐-如何加强固定资产管理?怎么加强固定资产管理?

【面试高频】cookie、session、token?看完再也不担心被问了

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

Yunna | how to manage the fixed assets of the company? How to manage the company's fixed assets better?

C语言项目实战:24点游戏计算器(基于结构体、指针、函数、数组、循环等知识点)
随机推荐
云呐|公司固定资产如何管理?公司固定资产如何管理比较好?
Work notes: one time bag grabbing
Which is a good fixed asset management system? What are the fixed asset management platforms?
基于EFR32MG24的AI 加速度姿势识别体验
cmake笔记
452. Detonate the balloon with the minimum number of arrows
运维高级作业03
Cool code rain dynamic background registration page
【软件测试】如何梳理你测试的业务
Ffmpeg 2 - use of ffplay, ffprobe, ffmpeg commands
直播课堂系统03补充-model类及实体
FastAPI应用加入Nacos
在使用 VScode 进行代码格式化后,保存发现代码又变乱了,怎么办?vs去掉格式化
[C language] number guessing game + shutdown applet
JS texture style pie chart plug-in
CSDN writing method (II)
运维高级作业02
Generate order number
websocket通用化封装设计与实现
What is per title encoding?