当前位置:网站首页>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;
}
边栏推荐
- LeetCode 61. 旋转链表
- OSSCore 开源解决方案介绍
- 阻塞 非阻塞 poll机制 异步
- 力扣练习——56 寻找右区间
- Where can I view the version record of WeChat applet submission review history?
- Stroke Practice - 62 Valid Sudokus
- 杭电多校-Loop-(不确定性贪心+线段树)
- 实现内网穿透的最佳解决方案(无实名认证,完全免费)
- Not just running away, but saving the guy who mishandled rm -rf /*
- Chapter 22 Source Code File REST API Reference (4)
猜你喜欢
从源码角度分析UUID的实现原理
Where can I view the version record of WeChat applet submission review history?
【勇敢饭饭,不怕刷题之链表】链表中有环的问题
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
为什么Redis很快
机器学习之暴力调参案例
std::move()
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
项目部署、
Emulate stm32 directly with proteus - the programmer can be completely discarded
随机推荐
From the product dimension, why can't we fully trust Layer2?
基于UiAutomator2+PageObject模式开展APP自动化测试实战
ViT结构详解(附pytorch代码)
LeetCode_443_压缩字符串
负载均衡原理分析与源码解读
Spss-多元回归案例实操
Redis设计与实现
Nocalhost - Making development more efficient in the cloud-native era
Since the media hot style title how to write?Taught you how to write the title
HDU 1520 Anniversary party (tree dp)
Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
OSSCore 开源解决方案介绍
关于“码农”的一点自嘲解构
从产品维度来看 我们为什么不能完全信任Layer2?
力扣练习——62 有效的数独
【Untitled】
微信小程序提交审核历史版本记录从哪里查看
LAXCUS分布式操作系统安全管理
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
网络套接字(UDP和TCP编程)