当前位置:网站首页>力扣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;
}
}
边栏推荐
- Ramnit感染型病毒分析与处置
- 【分布式】分布式ID生成策略
- AXI interface application of Zynq Fpga image processing - the use of axi_lite interface
- LayaBox---TypeScript---structure
- In which industries is the PMP certificate useful?
- boostrap多选PID查找端口 window
- Codeforces Round #811 (Div. 3)
- LeetCode 199: 二叉树的右视图
- ES6高级-Promise的用法
- 2022强网杯web(部分)
猜你喜欢
随机推荐
EasyGBS接入最新版海康摄像头后无法传递告警信息该如何解决?
Develop your own text recognition application with Tesseract
C language knowledge (1) - overview of C language, data types
ROS packages visualization
Rocketchip RISC-V Debug调试硬件相关(四)hartIsInReset
遍历await方法的区别:以for和forEach为例
Flutter 实现背景图片毛玻璃效果
ini怎么使用? C#教程
1319_STM32F103串口BootLoader移植
idea 仓库地址连接不上问题
LeetCode143:重排链表
Red team kill-free development practice of simulated confrontation
Milvus configuration related
立即升级!WPS Office 出现 0day 高危安全漏洞:可完全接管系统,官方推出紧急更新
中大型商业银行堡垒机升级改造方案!必看!
如何一键重装win7系统?重装win7系统详细教程
AI/ML无线通信
强网杯2022——WEB
LocalDateTime的详细使用方法
Arduino 电机测速








![[Linear Algebra 02] 2 interpretations of AX=b and 5 perspectives of matrix multiplication](/img/38/764b447cf7d886500a9b99d7679cb6.png)
