当前位置:网站首页>[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;
}
}
边栏推荐
- Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
- 3DMax指定面贴图
- CCNA-思科网络 EIGRP协议
- config:invalid signature 解决办法和问题排查详解
- GD32F4XX串口接收中断和闲时中断配置
- Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
- Aardio - 不声明直接传float数值的方法
- Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
- AdaViT——自适应选择计算结构的动态网络
- 2500 common Chinese characters + 130 common Chinese and English characters
猜你喜欢
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
Senior soft test (Information System Project Manager) high frequency test site: project quality management
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
Mysql database basic operations DML
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
Management background --5, sub classification
Pit encountered by handwritten ABA
【LeetCode】19、 删除链表的倒数第 N 个结点
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
随机推荐
Management background --3, modify classification
二叉(搜索)树的最近公共祖先 ●●
Aardio - 封装库时批量处理属性与回调函数的方法
The nearest common ancestor of binary (search) tree ●●
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
Applet system update prompt, and force the applet to restart and use the new version
NPDP certification | how do product managers communicate across functions / teams?
C#实现水晶报表绑定数据并实现打印4-条形码
C#實現水晶報錶綁定數據並實現打印4-條形碼
Solve project cross domain problems
2022-07-05 stonedb sub query processing parsing time analysis
Management background --4, delete classification
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
config:invalid signature 解决办法和问题排查详解
Pit encountered by handwritten ABA
Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
NetXpert XG2帮您解决“布线安装与维护”难题
Web APIs DOM 时间对象
软考高级(信息系统项目管理师)高频考点:项目质量管理
Crawler obtains real estate data