当前位置:网站首页>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
边栏推荐
- Rain produces hundreds of valleys, and all things grow
- Realizing data value through streaming data integration (5) - stream processing
- Zhengda international explains what the Dow Jones industrial index is?
- DBA常用SQL语句(1)— 概况信息
- [untitled]
- DBA常用SQL语句(3)- cache、undo、索引和等待事件
- 杰理之用户如何最简单的处理事件【篇】
- Go语言实践模式 - 函数选项模式(Functional Options Pattern)
- SQL tuning series - SQL performance methodology
- Juc并发编程06——深入剖析队列同步器AQS源码
猜你喜欢

0704、ansible----01

Nvidia最新三维重建技术Instant-ngp初探

Arm debugging (1): two methods to redirect printf to serial port in keil

Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022

Planning and construction of industrial meta universe platform

The central control learning infrared remote control module supports network and serial port control

Sim Api User Guide(4)

Configuration of LNMP

【无标题】

shell脚本免交互
随机推荐
Mobius inversion
Common DBA SQL statements (4) - Top SQL
解决VMware卸载后再安装出现的问题
通过流式数据集成实现数据价值(5)- 流分析
Nine abilities of agile manufacturing in the era of meta universe
第二章 Oracle Database In-Memory 体系结构(上) (IM-2.1)
2022年广东省安全员A证第三批(主要负责人)考试试题及答案
转:毛姆:阅读是一座随身携带的避难所
CSP certification 202203-2 travel plan (multiple solutions)
ansible playbook语法和格式 自动化云计算
[COCI] Vje š TICA (subset DP)
Ansible playbook syntax and format automate cloud computing
0704、ansible----01
Sim Api User Guide(5)
Function realization of printing page
Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
Odoo server setup notes
Sim Api User Guide(8)
Construire neuf capacités de fabrication agile à l'ère métacosmique
实践六 Windows操作系统安全攻防