当前位置:网站首页>LeetCode 19. 删除链表的倒数第 N 个结点
LeetCode 19. 删除链表的倒数第 N 个结点
2022-08-10 11:09:00 【水菜笔】
原题网址https://leetcode.cn/problems/remove-nth-node-from-end-of-list/
删除倒数第n个节点;
public ListNode removeNthFromEnd(ListNode head, int n) {
// 可能删除的是第一个,所以引用虚拟节点
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode cur = dummyHead;
ListNode prev = dummyHead;
while(cur != null) {
// 要差n+1个节点,所以这里是n<0的条件
if(n<0) {
prev = prev.next;
}
n--;
cur = cur.next;
}
prev.next = prev.next.next;
return dummyHead.next;
}
边栏推荐
- 皕杰报表在传参乱码
- 老板加薪!看我做的WPF Loading!!!
- The brave rice rice, does not fear the brush list of 】 list has a ring
- Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
- 【机器学习】浅谈正规方程法&梯度下降
- 力扣练习——58 验证二叉搜索树
- 2022年裁员潮,失业程序员何去何从?
- 软件架构简介
- 英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
- Since the media hot style title how to write?Taught you how to write the title
猜你喜欢
如何使用工程仪器设备在线监测管理系统
The brave rice rice, does not fear the brush list of 】 list has a ring
Emulate stm32 directly with proteus - the programmer can be completely discarded
电脑怎么设置屏幕息屏时间(日常使用分享)
从源码角度分析UUID的实现原理
皕杰报表在传参乱码
[Go WebSocket] 多房间的聊天室(一)思考篇
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
微信小程序提交审核历史版本记录从哪里查看
ViT结构详解(附pytorch代码)
随机推荐
gpu-admission 源码分析
微信小程序提交审核历史版本记录从哪里查看
LeetCode 362. Design Hit Counter(计数器)
软件架构简介
Chapter 22 Source Code File REST API Reference (4)
做自媒体月入几万?博主们都在用的几个自媒体工具
再有人问你分布式事务,把这篇扔给他
实现内网穿透的最佳解决方案(无实名认证,完全免费)
codevs 2370 小机房的树 (LCA)
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
A case of violent parameter tuning in machine learning
3款不同类型的自媒体免费工具,有效提高创作、运营效率
接口定义与实现
LCD驱动端与设备端名称匹配过程分析(Tiny4412)
负载均衡原理分析与源码解读
Centos7环境使用Mysql离线安装包安装Mysql5.7
OSSCore 开源解决方案介绍
OPNsense安装配置Zenarmor
2022年裁员潮,失业程序员何去何从?
模块九 - 设计电商秒杀系统