当前位置:网站首页>[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;
}
}

边栏推荐
- 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
- 墨西哥一架飞往美国的客机起飞后遭雷击 随后安全返航
- 【雅思口语】安娜口语学习记录part1
- 12、 Start process
- Advantages of link local address in IPv6
- LeetCode刷题(十一)——顺序刷题51至55
- Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
- C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
- i.mx6ull搭建boa服务器详解及其中遇到的一些问题
- 二分图判定
猜你喜欢

Memorabilia of domestic database in June 2022 - ink Sky Wheel

自制J-Flash烧录工具——Qt调用jlinkARM.dll方式

Export MySQL table data in pure mode

2500 common Chinese characters + 130 common Chinese and English characters

剑指offer刷题记录1

pytorch_YOLOX剪枝【附代码】

2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行

Attack and defense world ditf Misc

0 basic learning C language - digital tube

CCNA Cisco network EIGRP protocol
随机推荐
i. Mx6ull build boa server details and some of the problems encountered
Clip +json parsing converts the sound in the video into text
(18) LCD1602 experiment
中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
2022-07-05 stonedb sub query processing parsing time analysis
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
2022-07-05 stonedb的子查询处理解析耗时分析
MySQL数据库基本操作-DML
volatile关键字
小常识:保险中的“保全”是什么?
基於 QEMUv8 搭建 OP-TEE 開發環境
Shortcut keys in the terminal
枚举与#define 宏的区别
Lora sync word settings
新手程序员该不该背代码?
go多样化定时任务通用实现与封装
做接口测试都测什么?有哪些通用测试点?
GD32F4XX串口接收中断和闲时中断配置
Pit encountered by handwritten ABA