当前位置:网站首页>【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;
}
}
边栏推荐
- 在IPv6中 链路本地地址的优势
- MySQL----初识MySQL
- Anaconda installs third-party packages
- 中国固态氧化物燃料电池技术进展与发展前景报告(2022版)
- 硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
- Mise en place d'un environnement de développement OP - tee basé sur qemuv8
- 解决项目跨域问题
- Force buckle 575 Divide candy
- Management background --1 Create classification
- 第3章:类的加载过程(类的生命周期)详解
猜你喜欢
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
labelimg的安装与使用
Pit encountered by handwritten ABA
基于 QEMUv8 搭建 OP-TEE 开发环境
2500个常用中文字符 + 130常用中英文字符
Mise en place d'un environnement de développement OP - tee basé sur qemuv8
Management background --1 Create classification
[线性代数] 1.3 n阶行列式
小常识:保险中的“保全”是什么?
Aardio - 利用customPlus库+plus构造一个多按钮组件
随机推荐
MySQL约束的分类、作用及用法
[sciter bug] multi line hiding
How do I write Flask's excellent debug log message to a file in production?
Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
Inno Setup 打包及签名指南
Force deduction question 500, keyboard line, JS implementation
Mysql database basic operations DML
OpenCV VideoCapture. Get() parameter details
Aardio - 不声明直接传float数值的方法
每日一题:力扣:225:用队列实现栈
Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
sizeof关键字
i.mx6ull搭建boa服务器详解及其中遇到的一些问题
Senior soft test (Information System Project Manager) high frequency test site: project quality management
剪映+json解析将视频中的声音转换成文本
[sciter]: encapsulate the notification bar component based on sciter
(十八)LCD1602实验
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
变量与“零值”的比较