当前位置:网站首页>【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;
}
}
边栏推荐
- Crawler obtains real estate data
- Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
- [10:00 public class]: basis and practice of video quality evaluation
- Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
- General implementation and encapsulation of go diversified timing tasks
- (十八)LCD1602实验
- volatile关键字
- HDR image reconstruction from a single exposure using deep CNN reading notes
- 新手程序员该不该背代码?
- The nearest common ancestor of binary (search) tree ●●
猜你喜欢
NPDP certification | how do product managers communicate across functions / teams?
Oracle-控制文件及日志文件的管理
Pit encountered by handwritten ABA
pytorch_ Yolox pruning [with code]
手写ABA遇到的坑
LeetCode 练习——剑指 Offer 26. 树的子结构
Chapter 4: talk about class loader again
C#实现水晶报表绑定数据并实现打印4-条形码
二叉(搜索)树的最近公共祖先 ●●
数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
随机推荐
Senior soft test (Information System Project Manager) high frequency test site: project quality management
return 关键字
Management background --4, delete classification
anaconda安装第三方包
Classification, function and usage of MySQL constraints
Daily question 1: force deduction: 225: realize stack with queue
Spatial domain and frequency domain image compression of images
LeetCode 练习——剑指 Offer 26. 树的子结构
Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
[sciter]: encapsulate the notification bar component based on sciter
signed、unsigned关键字
414. The third largest digital buckle
How do I write Flask's excellent debug log message to a file in production?
【雅思口语】安娜口语学习记录part1
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
Build op-tee development environment based on qemuv8
How does the uni admin basic framework close the creation of super administrator entries?
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
[sciter bug] multi line hiding
Data storage (1)