当前位置:网站首页>[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;
}
}
边栏推荐
- Inno setup packaging and signing Guide
- 12、 Start process
- MySQL----初识MySQL
- CCNA Cisco network EIGRP protocol
- 小常识:保险中的“保全”是什么?
- Spatial domain and frequency domain image compression of images
- SQL server generates auto increment sequence number
- AI 企业多云存储架构实践 | 深势科技分享
- Assembly and interface technology experiment 5-8259 interrupt experiment
- NPDP certification | how do product managers communicate across functions / teams?
猜你喜欢
Self made j-flash burning tool -- QT calls jlinkarm DLL mode
Installation and use of labelimg
Barcodex (ActiveX print control) v5.3.0.80 free version
signed、unsigned关键字
That's why you can't understand recursion
AdaViT——自适应选择计算结构的动态网络
图像的spatial domain 和 frequency domain 图像压缩
Web APIs DOM time object
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
Memorabilia of domestic database in June 2022 - ink Sky Wheel
随机推荐
剑指offer刷题记录1
变量与“零值”的比较
柔性数组到底如何使用呢?
Aardio - 利用customPlus库+plus构造一个多按钮组件
中国固态氧化物燃料电池技术进展与发展前景报告(2022版)
extern关键字
2500 common Chinese characters + 130 common Chinese and English characters
2022年6月国产数据库大事记-墨天轮
Attack and defense world miscall
Assembly and interface technology experiment 5-8259 interrupt experiment
Anaconda installs third-party packages
Oracle control file and log file management
Export MySQL table data in pure mode
0 basic learning C language - digital tube
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
Inno setup packaging and signing Guide
第3章:类的加载过程(类的生命周期)详解
Memorabilia of domestic database in June 2022 - ink Sky Wheel
Barcodex (ActiveX print control) v5.3.0.80 free version
pytorch_YOLOX剪枝【附代码】