当前位置:网站首页>【LeetCode】19、 删除链表的倒数第 N 个结点
【LeetCode】19、 删除链表的倒数第 N 个结点
2022-07-06 14:47:00 【小曲同学呀】
19.、删除链表的倒数第 N 个结点
题目:
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
示例 1:
输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
示例 2:
输入:head = [1], n = 1
输出:[]
示例 3:
输入:head = [1,2], n = 1
输出:[1]
提示:
链表中结点的数目为 sz
1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
解题思路:
此题LeetCode评的难度为中等,我觉得这题比简单的还要简单。
为什么这么说呢?
链表删除某个位置上的节点,这难吗?
大家可以找一下规律。因为题中要求删除倒数第n个节点
官方题解给的是计算链表长度、栈和双指针。
这里用一个新的思路,递归!寻找删除的位置链表的长度。
简单粗暴
具体代码:
/** * 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;
}
}
边栏推荐
- Leetcode exercise - Sword finger offer 26 Substructure of tree
- Aardio - 不声明直接传float数值的方法
- Advantages of link local address in IPv6
- AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
- 414. The third largest digital buckle
- How does the uni admin basic framework close the creation of super administrator entries?
- Aardio - 封装库时批量处理属性与回调函数的方法
- i.mx6ull搭建boa服务器详解及其中遇到的一些问题
- UDP编程
- Applet system update prompt, and force the applet to restart and use the new version
猜你喜欢
二叉(搜索)树的最近公共祖先 ●●
Management background --3, modify classification
第4章:再谈类的加载器
手写ABA遇到的坑
ZABBIX proxy server and ZABBIX SNMP monitoring
【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真
0 basic learning C language - digital tube
NetXpert XG2帮您解决“布线安装与维护”难题
Chapter 4: talk about class loader again
2021 geometry deep learning master Michael Bronstein long article analysis
随机推荐
Chapter 3: detailed explanation of class loading process (class life cycle)
Web APIs DOM 时间对象
A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely
The nearest common ancestor of binary (search) tree ●●
Leetcode exercise - Sword finger offer 26 Substructure of tree
Management background --5, sub classification
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
Spatial domain and frequency domain image compression of images
(18) LCD1602 experiment
Attack and defense world miscall
小程序系统更新提示,并强制小程序重启并使用新版本
Mysql database basic operations DML
0 basic learning C language - interrupt
小常识:保险中的“保全”是什么?
十二、启动流程
第4章:再谈类的加载器
Senior soft test (Information System Project Manager) high frequency test site: project quality management
Oracle control file and log file management
case 关键字后面的值有什么要求吗?
How does the uni admin basic framework close the creation of super administrator entries?