当前位置:网站首页>力扣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感染型病毒分析与处置
- LayaBox---TypeScript---Problems encountered at first contact
- LocalDateTime的详细使用方法
- How to solve the problem that the alarm information cannot be transmitted after EasyGBS is connected to the latest version of Hikvision camera?
- input事件中文触发多次问题研究php DEBUG
- 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)
- The use and principle of CountDownLatch
- 立即升级!WPS Office 出现 0day 高危安全漏洞:可完全接管系统,官方推出紧急更新
- PyTorch Geometric (PyG) 安装教程
- The upgrade and transformation plan of the fortress machine for medium and large commercial banks!Must see!
猜你喜欢
js data type, throttling/anti-shake, click event delegation optimization, transition animation
Qiangwang Cup 2022 - WEB
DGL安装教程
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd6 in position 120: invalid continuation byte
[larave]关于laravel使用form submit()不能获取值问题
PowerCLi batch configuration of NTP
如何将二叉搜索树转化为一个有序的双向链表(原树上修改)
docker 搭建mysql 主从复制
LayaBox---TypeScript---Problems encountered at first contact
As hot as ever, reborn | ISC2022 HackingClub White Hat Summit was successfully held!
随机推荐
Flutter 实现背景图片毛玻璃效果
CountDownLatch使用及原理
关于std::vector<std::string>的操作
UDP通信
Cocoa Application-基础
零基础都能拿捏的七夕浪漫代码,快去表白或去制造惊喜吧
VSCode—常用快捷键(持续记录
LayaBox---TypeScript---structure
[Linear Algebra 03] Elimination method display and 4 solutions of AX=b
Moke, dynamic image resource package display
ES6高级-Promise的用法
Codeforces Round #811 (Div. 3)
docker 部署redis集群
Win11如何开启Telnet客户端?
Webmine Webpage Mining Trojan Analysis and Disposal
AI/ML无线通信
【uiautomation】微信好友列表获取(存储到txt中)
【ubuntu20.04安装MySQL以及MySQL-workbench可视化工具】
AtCoder Beginner Contest 262 D - I Hate Non-integer Number
Numpy on the superposition of two arrays