当前位置:网站首页>力扣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;
}
}
边栏推荐
- OC-类簇
- input事件中文触发多次问题研究php DEBUG
- 大势所趋之下的nft拍卖,未来艺术品的新赋能
- 数电快速入门(五)(编码器的介绍以及通用编码器74LS148和74LS147的介绍)
- LeetCode143:重排链表
- Exploration and Practice of Database Governance
- The use and principle of CountDownLatch
- buu web
- DSPE-PEG-Aldehyde, DSPE-PEG-CHO, Phospholipid-Polyethylene Glycol-Aldehyde A hydrophobic 18-carbon phospholipid
- "Jianzhi offer" brush title classification
猜你喜欢
Develop your own text recognition application with Tesseract
[QT] Implementation of callback function
rk3399-9.0 first-level and second-level dormancy
Is the International Project Manager PMP certificate worth taking?
Axure9基本交互操作(一)
如何一键重装win7系统?重装win7系统详细教程
PowerCLi import license to vCenter 7
Debian防火墙的开关以及状态
Red team kill-free development practice of simulated confrontation
EasyGBS接入最新版海康摄像头后无法传递告警信息该如何解决?
随机推荐
[Linear Algebra 02] 2 interpretations of AX=b and 5 perspectives of matrix multiplication
ES6高级-async的用法
LocalDateTime的详细使用方法
【SQL之降龙十八掌】01——亢龙有悔:入门10题
搬走地下空间开发利用“绊脚石” 中地数码取得地下空间透明化技术突破
论文解读(PPNP)《Predict then Propagate: Graph Neural Networks meet Personalized PageRank》
In action: 10 ways to implement delayed tasks, with code!
torch单机多卡和多机多卡训练
8 年产品经验,我总结了这些持续高效研发实践经验 · 协同篇
idea 仓库地址连接不上问题
遍历await方法的区别:以for和forEach为例
Several ways for rk3399 to drive screen parameters
AtCoder Beginner Contest 262 D - I Hate Non-integer Number
【QT】回调函数的实现
input事件中文触发多次问题研究php DEBUG
传奇服务器需要什么配置?传奇服务器租用价格表
【分布式】分布式ID生成策略
Oracle使用expdp和impdp导出导入数据
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)
Altium Designer 19.1.18 - Protecting Locked Objects