当前位置:网站首页>删除链表的倒数第N个节点---2022/02/22
删除链表的倒数第N个节点---2022/02/22
2022-06-11 17:18:00 【城猪猪】
题目描述:
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
JAVA实现如下:
package czz;
/* * 日期:2022.02.22 * 作者:城主 * 题目:给定一个链表删除倒数第n个节点并返回头节点 * 思考:由于java和C还是有所区别,少了指针的概念,那么在 * java中使用何种结构来表示C语言中的指针概念? * * 清楚了节点之间的连接关系,由于是倒数第n个节点,所以, * 首先定位到第len-n个位置的节点,直接做删除处理。 * * 收获: * 1、java中如何创建链表,在节点类中添加add的方法。 */
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val; }
ListNode(int val, ListNode next) {
this.val = val; this.next = next; }
//添加结点
public void add(int newdata){
ListNode newNode = new ListNode(newdata); //创建一个结点
if(this.next == null){
this.next = newNode;
}
else{
this.next.add(newdata);
}
}
}
public class DeleteNode_0222 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] str= {
1,2,3,4,5};
int n=2;
ListNode head=new ListNode(1,null);
for(int i=1;i<str.length;i++) {
head.add(str[i]);
}
}
public static ListNode removeNthFromEnd(ListNode head, int n) {
/* * 定义快慢指针,快指针先走N步,如果走到末端说明去除头一个节点 * 此时直接做返回。否则,快指针继续走到末端,然后慢指针走到待删 * 除节点的前一个节点。而后直接做删除处理。 */
ListNode fast=head;
ListNode slow=head;
while(n--!=0)
{
fast=fast.next;
}
if(fast==null)
{
return head.next;
}
fast=fast.next;
while(fast!=null)
{
fast=fast.next;
slow=slow.next;
}
slow.next=slow.next.next;
return head;
}
}
性能评估如下:
以内存换取时间。
边栏推荐
- 信息安全数学基础 Chapter 2——同余
- Analysis report on competition pattern and future development strategy of China's corn industry 2022-2028 Edition
- Go path: goroot & gopath
- 如何成为一个乐观派组织?
- av_read_frame返回值为-5 Input/output error
- Activity | authing's first channel cooperation activity came to a successful conclusion
- GUI guess number game, directly open play
- CLP information -5 keywords to see the development trend of the financial industry in 2022
- Remove key lookup bookmark from SQL Server
- Bentley 使用 Authing 快速实现应用系统与身份的集成
猜你喜欢

Guide to Dama data management knowledge system: percentage of chapter scores

Xie Yang, CEO of authing, was selected into Forbes' 30 under 30 Asia list in 2021

Connect the server with springboard / fortress through xshell

Bentley uses authing to quickly integrate application system and identity

Create database instance
![[MySQL] detailed explanation of redo log, undo log and binlog (4)](/img/67/6e646040c1b941c270b3efff74e94d.png)
[MySQL] detailed explanation of redo log, undo log and binlog (4)

为什么udp流设置1316字节

拜登下令强制推行零信任架构

Chip mass production, oppo entering a new era?

How to simplify a lot of if... Elif... Else code?
随机推荐
error:指针作为函数参数错误总结
Typescipt Basics
Vscode automatic eslint formatting when saving code
threejs中设置物体的贴图+场景的6面贴图 +创建空间
How does Sister Feng change to ice?
Vscode configures eslint to automatically format with an error "the setting is deprecated. use editor.codeactionsonsave instead with a source“
LeetCode——24. Exchange the nodes in the linked list in pairs (three pointers)
子类继承了什么、多态、 向上转型
6-3 读文章(*)
RecyclerView缓存复用解析,源码解读
Authing CEO 谢扬入选福布斯 2021 年 30 Under 30 亚洲榜单
Analysis report on competition pattern and future development strategy of China's corn industry 2022-2028 Edition
Docker安装mysql5.7(开启binlog功能、修改字符)
Guide to Dama data management knowledge system: percentage of chapter scores
05_特征工程—降维
Export data prompt -- solution to the problem of secure file priv option
Arraylist集合、对象数组
Error: error summary of pointer as function parameter
通过Xshell连接有跳板机/堡垒机的服务器
Create database instance