当前位置:网站首页>力扣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;
}
}
边栏推荐
猜你喜欢
【线性代数02】AX=b的2种解释和矩阵乘法的5种视角
EasyGBS接入最新版海康摄像头后无法传递告警信息该如何解决?
y87.第五章 分布式链路追踪系统 -- 分布式链路追踪系统起源(一)
ES 数据聚合、数据同步、集群
[larave]关于laravel使用form submit()不能获取值问题
七夕特制:《牛郎会织女》
Several ways for rk3399 to drive screen parameters
js data type, throttling/anti-shake, click event delegation optimization, transition animation
Redis中的LRU算法
unity2D横版游戏教程8-音效
随机推荐
【PCBA program design】Grip dynamometer program
ROS packages visualization
打卡第 2 天: urllib简记
如何一键重装win7系统?重装win7系统详细教程
How to solve the problem that the alarm information cannot be transmitted after EasyGBS is connected to the latest version of Hikvision camera?
《剑指offer》刷题分类
SPSS-unary regression practice
Flutter 实现背景图片毛玻璃效果
Analysis and treatment of Ramnit infectious virus
Numpy on the superposition of two arrays
如何为Web3.0世界启动完美的DAO
Altium Designer 19.1.18 - draw polygons copper hollow out, for the cursor just capture solutions
Exploration and Practice of Database Governance
[QT] Implementation of callback function
idea 仓库地址连接不上问题
七夕,当爱神丘比特遇上牛郎和织女
开源一夏 | 云服务器ECS安装Mysql、JDK、RocketMQ
Open source summer | Cloud server ECS installs Mysql, JDK, RocketMQ
如何将二叉搜索树转化为一个有序的双向链表(原树上修改)
Chapter7 : Network-Driven Drug Discovery