当前位置:网站首页>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;
}
}

边栏推荐
- Now CDC supports MySQL 5 What time is it? Previously, it seemed that it was 5.7. Today, it is found that the MySQL data source of 5.6 can also be updated in real time
- 【干货知识】Redis:从应用到底层,一文帮你搞定
- Zabbix6.0+timescaledb+ enterprise wechat alarm
- 大魚吃小魚小遊戲完整版
- JMeter thread duration
- 1156 Sexy Primes
- Write down some pat topics (I)
- 麻烦问下,数据集成中的独享资源,无法正常连接某些数据库,可能是什么原因,以下的原因好像都不是。公共资
- [wechat applet] collaboration and publishing data binding
- 范畴(Category)
猜你喜欢

最高月薪17K,只要心中有一片希望的田野,勤奋耕耘将迎来一片翠绿~

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

mysql如何实现分组求和

Category

How to query all tables in MySQL

Recycleview lazy load failure (II)

5月刚刚阿里面软件测试岗回来,3+1面任职阿里P7,年薪28*15薪
![[complete course of time series prediction] take temperature prediction as an example to illustrate the composition of the paper and the construction of pytorch code pipeline](/img/fe/c204ea78051fb0544b27d44e6d1297.png)
[complete course of time series prediction] take temperature prediction as an example to illustrate the composition of the paper and the construction of pytorch code pipeline

Clustering, dimension reduction and measurement techniques for machine learning

大魚吃小魚小遊戲完整版
随机推荐
日常开发常用工具提升效率
SD集训6.21总结
传iPhone 14将全系涨价;TikTok美国用户数据转移到甲骨文,字节无法访问;SeaTunnel 2.1.2发布|极客头条
NetCore3.1 ping网络是否畅通及获取服务器Cpu、内存使用率
《跟老卫学 HarmonyOS 开发》:以父之名·码力全开!写段HarmonyOS祝父亲节
ArrayList源码解析
07_那些可以改变你认知的知识
2022-06-20
inno setup 安装路径框学习
【干货知识】Redis:从应用到底层,一文帮你搞定
How to query the data in MySQL
mysql如何查询第几条数据
Novice uses apiccloud visual development to build the mall home page
[wechat applet failed to change appid] wechat applet failed to modify appid all the time and reported an error. Tourist appid solution
Manjaro installs the downloaded TTF font file
散户买基金哪个平台最好最安全
When the move protocol beta is in progress, the ecological core equity Momo is divided
自定义代码模板
某大厂第二轮裁员来袭,套路满满
MySQL-CentOS安装MySQL8