当前位置:网站首页>19、删除链表的倒数第N个节点(链表)
19、删除链表的倒数第N个节点(链表)
2022-04-23 10:11:00 【Popuessing's Jersey】
题目:给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
进阶:你能尝试使用一趟扫描实现吗?
public class ShanchulianbiaodedaoshudiNgejiedian {
public ListNode removeNthfromEnd(ListNode head,int n){
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode left = dummy;
ListNode right = dummy;
//让快指针先走n步,然后再让快慢指针一起走,
//直到快指针走到空,此时慢指针指向的就是倒数第n个节点(但是这样不方便做删除操作)
//删除操作需要找到被删除对象的前一个节点,因此快指针可以先走n+1步,那么慢指针到时候指向的节点就是倒数n+1个节点了
for (int i = 0; i <=n ; i++) {
if(right.next!=null) {
right = right.next;
}
}
//寻找了倒数第n+1个节点
while (right!=null){
left = left.next;
right = right.next;
}
//删除倒数第n个节点
left.next = left.next.next;
return dummy.next;
}
//打印输出
static void print(ListNode listNode){
//创建链表节点
while (listNode!=null){
if(listNode.next==null){
System.out.print(listNode.val);
}else {
System.out.print(listNode.val + "->");
}
listNode = listNode.next;
}
}
public static void main(String[] args) {
//创建首节点
ListNode node = new ListNode(1);
//定义移动节点
ListNode nextnode = node;
for (int i = 2; i <=6 ; i++) {
//生成新节点
ListNode nodex = new ListNode(i);
//移动节点指向新节点(连接新节点
nextnode.next = nodex;
//把当前节点往后移动
nextnode = nextnode.next;
}
//链表生成完毕
//直接从首节点打印
print(node);
System.out.println();
ShanchulianbiaodedaoshudiNgejiedian shanchulianbiaodedaoshudiNgejiedian = new ShanchulianbiaodedaoshudiNgejiedian();
ListNode res = shanchulianbiaodedaoshudiNgejiedian.removeNthfromEnd(node,3);
print(res);
}
}
输出结果:
1->2->3->4->5->6
1->2->3->5->6
时间复杂度:O(n)
空间复杂度:O(1)
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://blog.csdn.net/CoCo629vanilla/article/details/121411610
边栏推荐
- [untitled]
- Go语言实践模式 - 函数选项模式(Functional Options Pattern)
- 转:毛姆:阅读是一座随身携带的避难所
- [COCI] lattice (dichotomy + tree divide and conquer + string hash)
- CSP认证 202203-2 出行计划(多种解法)
- Classic routine: DP problem of a kind of string counting
- Ansible playbook syntax and format automate cloud computing
- Common DBA SQL statements (4) - Top SQL
- Introduction to graph theory -- drawing
- [lnoi2014] LCA - tree chain subdivision - multipoint LCA depth and problems
猜你喜欢
Solve the problem of installing VMware after uninstalling
Sim Api User Guide(5)
防疫登记小程序
从知识传播的维度对比分析元宇宙
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
《谷雨系列》空投
The central control learning infrared remote control module supports network and serial port control
MapReduce核心和基础Demo
Juc并发编程06——深入剖析队列同步器AQS源码
随机推荐
GCD of p2257 YY (Mobius inversion)
防疫登记小程序
Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
Common DBA SQL statements (4) - Top SQL
CSP认证 202203-2 出行计划(多种解法)
NEC infrared remote control coding description
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
ansible 云计算 自动化 命令行精简版
Sim Api User Guide(5)
工业元宇宙平台规划与建设
Odoo 服务器搭建备忘
Career planning and implementation in the era of meta universe
Solve the problem of installing VMware after uninstalling
Yarn资源调度器
通过流式数据集成实现数据价值(1)
中控学习型红外遥控模块支持网络和串口控制
構建元宇宙時代敏捷制造的九種能力
1D / 1D dynamic programming learning summary
Prefix sum of integral function -- Du Jiao sieve
Go language practice mode - functional options pattern