当前位置:网站首页>[leetcode] 19. Delete the penultimate node of the linked list
[leetcode] 19. Delete the penultimate node of the linked list
2022-07-06 22:28:00 【Xiaoqu classmate】
19.、 Delete the last of the linked list N Nodes
subject :
I'll give you a list , Delete the last of the linked list n Nodes , And return the head node of the list .
Example 1:
Input :head = [1,2,3,4,5], n = 2
Output :[1,2,3,5]
Example 2:
Input :head = [1], n = 1
Output :[]
Example 3:
Input :head = [1,2], n = 1
Output :[1]
Tips :
The number of nodes in the list is sz
1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
Their thinking :
This question LeetCode The difficulty of evaluation is medium , I think this question is simpler than the simple one .
Why do you say that ?
The linked list deletes a node at a certain position , Is it difficult ?
You can find rules . Because it is required to delete the penultimate n Nodes
The official solution is to calculate the length of the linked list 、 Stack and double pointer .
Here is a new idea , recursive ! Find the length of the deleted location linked list .
Simple and crude
Specific code :
/** * 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 {
int i=0;
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head.next!=null)
head.next=removeNthFromEnd(head.next,n);
i++;
if(i==n)
return head.next;
else return head;
}
}
边栏推荐
- Gd32f4xx serial port receive interrupt and idle interrupt configuration
- 网络基础入门理解
- [linear algebra] determinant of order 1.3 n
- Export MySQL table data in pure mode
- Inno setup packaging and signing Guide
- pytorch_ Yolox pruning [with code]
- config:invalid signature 解决办法和问题排查详解
- 3DMAX assign face map
- Mise en place d'un environnement de développement OP - tee basé sur qemuv8
- Chapter 4: talk about class loader again
猜你喜欢
Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
Attack and defense world ditf Misc
config:invalid signature 解决办法和问题排查详解
第4章:再谈类的加载器
NPDP certification | how do product managers communicate across functions / teams?
Aardio - 不声明直接传float数值的方法
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
云原生技术--- 容器知识点
网络基础入门理解
HDR image reconstruction from a single exposure using deep CNNs阅读札记
随机推荐
The nearest common ancestor of binary (search) tree ●●
return 关键字
新手程序员该不该背代码?
2500 common Chinese characters + 130 common Chinese and English characters
MySQL----初识MySQL
图像的spatial domain 和 frequency domain 图像压缩
小程序系统更新提示,并强制小程序重启并使用新版本
Common sense: what is "preservation" in insurance?
SQL Server生成自增序号
柔性数组到底如何使用呢?
2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
[leetcode daily clock in] 1020 Number of enclaves
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
Export MySQL table data in pure mode
C # realizes crystal report binding data and printing 4-bar code
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
【雅思口语】安娜口语学习记录part1
Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真
AdaViT——自适应选择计算结构的动态网络