当前位置:网站首页>力扣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;
}
}
边栏推荐
- torch单机多卡和多机多卡训练
- 数字重塑客观世界,全空间GIS发展正当其时
- Some problems with passing parameters of meta and params in routing (can be passed but not passed, empty, collocation, click to pass multiple parameters to report an error)
- PCBA scheme design - kitchen voice scale chip scheme
- Develop your own text recognition application with Tesseract
- 基于 Milvus 和 ResNet50 的图像搜索(部署及应用)
- ES 数据聚合、数据同步、集群
- 【分布式】分布式ID生成策略
- Cocoa Application-基础
- Several ways for rk3399 to drive screen parameters
猜你喜欢
![[Linear Algebra 02] 2 interpretations of AX=b and 5 perspectives of matrix multiplication](/img/38/764b447cf7d886500a9b99d7679cb6.png)
[Linear Algebra 02] 2 interpretations of AX=b and 5 perspectives of matrix multiplication

【线性代数03】消元法展示以及AX=b的4种解情况

2022年江苏省大学生电子设计竞赛(TI杯)B题 飞机 省级一等奖记录 “一个摆烂人的独白”

ROS packages visualization

立即升级!WPS Office 出现 0day 高危安全漏洞:可完全接管系统,官方推出紧急更新

Arduino 电机测速

unity2D横版游戏教程8-音效

驱动点云格式修改带来的效率提升

论文解读(PPNP)《Predict then Propagate: Graph Neural Networks meet Personalized PageRank》

中大型商业银行堡垒机升级改造方案!必看!
随机推荐
开源一夏 | 云服务器ECS安装Mysql、JDK、RocketMQ
DGL安装教程
docker 部署redis集群
LayaBox---TypeScript---Problems encountered at first contact
LeetCode: 406. 根据身高重建队列
UDP通信
快速web开发框架——learun framework
基于声卡实现的音频存储示波器,可作为电磁学实验的测量仪表
Rocketchip RISC-V Debug调试硬件相关(四)hartIsInReset
如何为Web3.0世界启动完美的DAO
LocalDateTime的详细使用方法
ROS packages visualization
Yolov7:Trainable bag-of-freebies sets new state-of-the-art for real-time objectdetectors
《剑指offer》刷题分类
强网杯2022——WEB
As hot as ever, reborn | ISC2022 HackingClub White Hat Summit was successfully held!
How to solve the problem that the alarm information cannot be transmitted after EasyGBS is connected to the latest version of Hikvision camera?
rk3399-9.0 first-level and second-level dormancy
七夕特制:《牛郎会织女》
input事件中文触发多次问题研究php DEBUG
