当前位置:网站首页>Delete the penultimate node - linked list topic
Delete the penultimate node - linked list topic
2022-06-21 20:24:00 【ailigei】

First , Solve this problem , Delete the last k Nodes , The first thing that came to mind was the two pointer solution , for example , Delete the penultimate node , We can take a look at the general idea , First, you can define two pointers ,p1 and p2, With first pointer p1 go 2 Step , Then initialize the second pointer p2 Point to head node , Then let the two pointers traverse forward at the same speed at the same time . Since the distance between the two pointers is always kept at 2, When the pointer p1 When pointing to the tail node of the linked list p2 It's just pointing to the bottom of 3 Nodes , Then delete the penultimate node directly .
I started with the normal operation , Caused the array to be out of bounds , Maybe many people will fall into this kind of pit when they just get started with the algorithm .
import java.util.*;
/* * public class ListNode { * int val; * ListNode next = null; * } */
public class Solution {
/** * * @param head ListNode class * @param n int integer * @return ListNode class */
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
ListNode pre=head;
ListNode bre=head;
for(int i=0;i<n;i++)
{
pre=pre.next;
}
while(pre.next!=null)
{
pre=pre.next;
bre=bre.next;
}
bre.next=bre.next.next;
return head;
}
}
Later, it was found that the use of sentinel nodes can perfectly solve the problem of array out of bounds . You can compare the difference between the two codes . Fine products
import java.util.*;
/* * public class ListNode { * int val; * ListNode next = null; * } */
public class Solution {
/** * * @param head ListNode class * @param n int integer * @return ListNode class */
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
ListNode dummy=new ListNode(-1);
dummy.next=head;
ListNode front=head,back=dummy;
for(int i=0;i<n;i++)
{
front=front.next;
}
while(front!=null)
{
front=front.next;
back=back.next;
}
back.next=back.next.next;
return dummy.next;
}
}

边栏推荐
- Big Fish eating Little Fish Games version complète
- 大鱼吃小鱼小游戏完整版
- 金鱼哥RHCA回忆录:DO447Ansible Tower导航
- Whether there are keywords in MySQL
- What are the knowledge points of SQL statements
- Datagear uses coordinate mapping table to make geographic coordinate data visualization Kanban
- SQL语句知识点有哪些
- ArrayList源码解析
- Is it safe to open a margin account? What are the requirements?
- How MySQL implements grouping sum
猜你喜欢

【微信小程序】协同工作和发布 数据绑定

Tensorflow 2: use neural network to classify and compare fashion MNIST

Novice uses apiccloud visual development to build the mall home page

【微信小程序更改appid失败】微信小程序修改appid一直失败报错tourist appid解决办法

汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(一)——整体系统设计

inno setup 更改安装路径学习

大鱼吃小鱼小游戏完整版

Gradle download and installation configuration

LeetCode个人题解(剑指offer 21-25)21. 调整数组顺序使奇数位于偶数前面,22. 链表中倒数第k个节点,24. 反转链表,25. 合并两个排序的链表

机器学习和模式识别怎么区分?
随机推荐
[cvpr2022] CMU tutorial on multimodal machine learning, 200+ pages to explain the knowledge of multimodal learning system with six challenges of representation, alignment, reasoning, migration, genera
Selected articles of the research paper | interpretation of the trend of McKinsey's China's Digital Innovation future
Big Fish eating Little Fish Games version complète
inno setup 安装路径框学习
Flink CDC MongoDB Connector 的实现原理和使用实践
When the move protocol beta is in progress, the ecological core equity Momo is divided
SD training 6.21 summary
删除倒数第k个节点-链表专题
Gradle下载与安装配置
mysql如何查询最大id值
Model evaluation and selection of machine learning
2022-06-20
mysql如何对列求和
SD集训6.21总结
break和continue的区别
汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(二)——贪吃蛇详细设计
InfluxDB优化配置项
LeetCode个人题解(剑指offer 21-25)21. 调整数组顺序使奇数位于偶数前面,22. 链表中倒数第k个节点,24. 反转链表,25. 合并两个排序的链表
Zabbix6.0+timescaledb+ enterprise wechat alarm
SQL语句知识点有哪些