当前位置:网站首页>力扣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;
}
}
边栏推荐
猜你喜欢
Exploration and Practice of Database Governance

Driving point cloud format changes bring efficiency improvement

Numpy on the superposition of two arrays

Red team kill-free development practice of simulated confrontation
![[QT] Implementation of callback function](/img/df/500ae0625eccf83f39d9a8ddfd3533.png)
[QT] Implementation of callback function

【Social Marketing】WhatsApp Business API: Everything You Need to Know

2022强网杯web(部分)

立方度量(Cubic Metric)

Altium Designer 19.1.18 - Protecting Locked Objects

如何一键重装Win11系统 一键重装系统方法
随机推荐
ROS播包可视化
LeetCode: 406. 根据身高重建队列
openresty lua-resty-template页面静态化
27. Dimensionality reduction
论文解读(PPNP)《Predict then Propagate: Graph Neural Networks meet Personalized PageRank》
【PCBA program design】Grip dynamometer program
【Social Marketing】WhatsApp Business API: Everything You Need to Know
OC-归档(序列化)(了解的不多 没细看)
ES6高级-async的用法
Altium Designer 19.1.18 - 画多边形铜皮挖空时,针对光标胡乱捕获的解决方法
unity2D横版游戏教程9-对话框dialog
如何一键重装win7系统?重装win7系统详细教程
Altium Designer 19.1.18 - draw polygons copper hollow out, for the cursor just capture solutions
SPSS-unary regression practice
ES6高级-Promise的用法
PCBA scheme design - kitchen voice scale chip scheme
立即升级!WPS Office 出现 0day 高危安全漏洞:可完全接管系统,官方推出紧急更新
Open source summer | Cloud server ECS installs Mysql, JDK, RocketMQ
Go----Go 语言基础之标识符、关键字、命名规范、变量、常量
[QT] Implementation of callback function
