当前位置:网站首页>力扣19-删除链表的倒数第 N 个结点——链表
力扣19-删除链表的倒数第 N 个结点——链表
2022-08-04 21:53:00 【张怼怼√】
题目描述
给你一个链表,删除链表的倒数第 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 {
public ListNode removeNthFromEnd(ListNode head, int n) {
int len = 0;
ListNode h = head;
while(h != null){
len++;
h = h.next;
}
ListNode dummy = new ListNode(0,head);
ListNode cur = dummy;
for(int i = 0; i < len-n; i++){
cur = cur.next;
}
cur.next = cur.next.next;
return dummy.next;
}
}
边栏推荐
- unity2D横版游戏教程8-音效
- Qiangwang Cup 2022 - WEB
- The use and principle of CountDownLatch
- Debian防火墙的开关以及状态
- Named routes, the role of name in components
- Excel商业智能-Power BI电商数据分析实战
- [QT] Implementation of callback function
- 可视化工作流引擎开发OA系统,让企业少花冤枉钱
- Is the International Project Manager PMP certificate worth taking?
- 用Tesseract开发一个你自己的文字识别应用
猜你喜欢
Win11如何开启Telnet客户端?
ctfshow终极考核web654
ES6高级-Promise的用法
炽热如初 向新而生|ISC2022 HackingClub白帽峰会圆满举办!
PowerCLi batch configuration of NTP
第二讲 软件生命周期
Oracle增加表空间解决ORACLE ORA-01653: unable to extend table报错
[Linear Algebra 03] Elimination method display and 4 solutions of AX=b
Several ways for rk3399 to drive screen parameters
ROS packages visualization
随机推荐
Qiangwang Cup 2022 - WEB
VSCode—常用快捷键(持续记录
PowerBI真经连续剧
如何一键重装Win11系统 一键重装系统方法
开发deepstram的自定义插件,使用gst-dseaxmple插件进行扩充,实现deepstream图像输出前的预处理,实现图像自定义绘制图(精四)
关于std::vector<std::string>的操作
CountDownLatch使用及原理
PMP证书在哪些行业有用?
Driving point cloud format changes bring efficiency improvement
[QT] Implementation of callback function
传奇服务器需要什么配置?传奇服务器租用价格表
【PCBA program design】Grip dynamometer program
2022强网杯web(部分)
Autowired自动装配
开源一夏 | 云服务器ECS安装Mysql、JDK、RocketMQ
Codeforces Round #811 (Div. 3)
input事件中文触发多次问题研究php DEBUG
OC-拷贝
大势所趋之下的nft拍卖,未来艺术品的新赋能
[larave]关于laravel使用form submit()不能获取值问题