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

边栏推荐
- Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
- 中国固态氧化物燃料电池技术进展与发展前景报告(2022版)
- Classic sql50 questions
- 雅思口语的具体步骤和时间安排是什么样的?
- What are the interface tests? What are the general test points?
- 414. The third largest digital buckle
- Management background --3, modify classification
- The nearest common ancestor of binary (search) tree ●●
- 剪映+json解析将视频中的声音转换成文本
- go多样化定时任务通用实现与封装
猜你喜欢

数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据

C#实现水晶报表绑定数据并实现打印4-条形码

Should novice programmers memorize code?

Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案

C#實現水晶報錶綁定數據並實現打印4-條形碼

Management background --1 Create classification

Leetcode question brushing (XI) -- sequential questions brushing 51 to 55

Common sense: what is "preservation" in insurance?

重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
![[10:00 public class]: basis and practice of video quality evaluation](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[10:00 public class]: basis and practice of video quality evaluation
随机推荐
pytorch_ Yolox pruning [with code]
HDR image reconstruction from a single exposure using deep CNNs阅读札记
C#实现水晶报表绑定数据并实现打印4-条形码
Attack and defense world miscall
CCNA Cisco network EIGRP protocol
2022-07-05 使用tpcc对stonedb进行子查询测试
QT | UDP broadcast communication, simple use case
414. The third largest digital buckle
Pit encountered by handwritten ABA
【雅思口语】安娜口语学习记录part1
Installation and use of labelimg
How does the uni admin basic framework close the creation of super administrator entries?
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
C # realizes crystal report binding data and printing 4-bar code
void关键字
Mysql database basic operations DML
General implementation and encapsulation of go diversified timing tasks
网络基础入门理解
插入排序与希尔排序
小常识:保险中的“保全”是什么?