Leetcode NO.19 Remove Nth Node From End Of List && 移除链表倒数第n个节点

VScode远程连接开发嵌入式开发板 VScode远程连接开发调试嵌入式开发板 阅读详情

1.问题描述

给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

2.测试用例

示例 1

输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]

示例 2:

输入:head = [1], n = 1
输出:[]

示例 3:

输入:head = [1,2], n = 1
输出:[1]

3.提示

  • 链表中结点的数目为 sz
  • 1 <= sz <= 30
  • 0 <= Node.val <= 100
  • 1 <= n <= sz

**进阶:**你能尝试使用一趟扫描实现吗?

4.代码

1.暴力
code
 public ListNode removeNthFromEndWithForce(ListNode head, int n) {
        int count = 0;
        ListNode sentinel = new ListNode();
        sentinel.next = head;
        ListNode pre = sentinel;
        while (head != null) {
            head = head.next;
            count++;
        }
        for (int i = 0; i < count - n; i++) {
            pre = pre.next;
        }
        pre.next = pre.next.next;
        return sentinel.next;
}
复杂度
这种如果n比较大的比较高效
时间 O(n)
空间 O(1)
2.双重循环-栈
code
public ListNode removeNthFromEndWithStack(ListNode head, int n) {
        ListNode sentinel = new ListNode(-1);
        sentinel.next = head;
        // 定义栈
        Stack<ListNode> stack = new Stack<>();
        ListNode curr = sentinel;
        // 遍历链表,所有节点入栈
        while ( curr != null ){
            stack.push(curr);
            curr = curr.next;
        }
        // 依次弹栈,弹出N个
        for ( int i = 0; i < n; i++ ){
            stack.pop();
        }
        stack.peek().next = stack.peek().next.next;
        return sentinel.next;
}
复杂度
这种如果n比较小的比较高效
时间O(n)
空间O(n)
3.双指针
code
 public ListNode removeNthFromEndWithDoublePointer(ListNode head, int n) {
        ListNode sentinel = new ListNode();
        sentinel.next = head;
        ListNode rem = sentinel;
        ListNode end = sentinel;
        //1. 移动end节点到n
        for (int i = 0; i < n; i++) {
            end = end.next;
        }
        //2. 移动rem、end节点,直到end节点指向null
        while (end.next != null){
            end = end.next;
            rem = rem.next;
        }
        //3. 删除倒数n节点
        rem.next = rem.next.next;
        return sentinel.next;
}
复杂度
时间复杂度 O(n)
空间复杂度 O (1)
RK平台PCIE-Ethernet扩展:Intel I210移植与性能飞升之路 本文详细介绍了Intel I210千兆以太网控制器在瑞芯微RK平台的移植与优化过程。通过硬件适配、内核配置、驱动开发及性能调优四个关键环节,实现了RK平台的网络性能提升。硬件方面,重点说明了PCIE接口的规范连接与供电检查;软件方面,系统阐述了内核配置、设备树修改和驱动编译流程。在性能优化环节,详细介绍了中断优化、缓冲区设置和TCP/IP协议栈调优策略,并采用Iperf和Netperf工具进行量化测试验证。测试结果显示,优化后的网络吞吐量提升20%以上,延迟降低40%,为工业控制、智能安防等场景提供了可靠的 阅读详情

相关推荐

深度解析跨境支付之业务架构

详解跨境支付之业务架构

weixin_47493863的博客 1878

【LeetCode】详解删除链表的倒数第N个节点19. Remove Nth Node From End of ListGiven a linked list, remove the n-th

文章目录前言正文原题:思路1:思路2:总结 前言 一开始我以为今天这道题很简单,也很快通过了,但是看了一下讨论区的代码,怎么都跟我的不一样,后来才发现原来我的英文翻译错了,具体是怎么回事呢?接着往下看吧 正文 原题: 链接:删除链表的倒数第N个节点 Given a linked list, remove the n-th node from the end of list and retur...

Qonent的博客 1171

555定时器呼吸灯电路制作全攻略:从仿真到实物搭建(附避坑指南)

本文详细介绍了使用555定时器制作呼吸灯电路的完整流程,从基础原理到实物搭建,包括电路设计、元件选择、仿真验证、焊接技巧及调试优化。特别提供了避坑指南,帮助电子爱好者快速掌握这一经典DIY项目,实现美观的呼吸灯效果。

weixin_33734785的博客 402

LeetCode 19 Remove Nth Node From End of List

LeetCode 19 Remove Nth Node From End of List 题目链接 Given the head of a linked list, remove the nth node from the end of the list and return its head. Follow up: Could you do this in one pass? Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] E

旺崽的博客 2448

手把手教你玩转CPU微架构专栏--​​​​第二章:流水线——微架构的“效率引擎”​​​​2.4 超标量与超线程:如何突破“单指令流”的限制?

例如,Intel酷睿i5-13600K拥有6个性能核(P核,支持超线程,共12线程)和8个能效核(E核,无超线程,共8线程),兼顾多线程性能与能效比。通过引入“物理寄存器”替代“逻辑寄存器”(如x86的RAX、RBX),消除数据冒险中的“假依赖”(False Dependency)。引入“混合架构”(Performance Core+Efficient Core),大核(P核)专注高性能任务,小核(E核)处理后台任务;​:6发射超标量,IPC≈3.5(通过3D V-Cache优化缓存,进一步提升效率)。

xiaoheshang_123的博客 955

一文了解ECU软件刷写的2种方法:Bootloader刷写和调试器刷写

分为刷写前、刷写中和刷写后三个阶段,如下示意:下面我们来详细了解一下Bootloader收到外部请求后开始刷写的三个阶段1)刷写前进行刷写的准备,一是确保刷写阶段总线通信的稳定性,二是避免刷写阶段出现非预期的故障,三是读取车辆数据供刷写前的一些检查等,如下所示:首先进行会话控制,通过功能寻址方式进入扩展会话模式,为后面的信息访问及其他指令做准备;然后检查刷写的前提条件,通过物理寻址方式发送历程控制请求。比如车确实处于静止状态,供电电压稳定等条件下才允许刷写;

menghuyouyou的博客 5784

LeetCode 19 Remove Nth Node From End of List 移除倒数第N个节点

题目: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the

大三狗现在才努力还不算晚吧?! 2689

Python+gensim【中文LDA】简洁模型

文章目录完整代码过程详解words_lsdictionarydictionary.doc2bow函数corpuslda 完整代码 from gensim import corpora, models import jieba.posseg as jp # 待分析文本集 text1 = '美国教练坦言,没输给中国女排,是输给了郎平' text2 = '中国女排世界排名第一?真实水平如何,听听巴西和美...

ArYe 2万+

【LeetCode系列】移除链表倒数第N个结点 Remove Nth Node From End of List

题目描述:LeetCode原题地址 Given a linked list, remove the n-th node from the end of list and return its head. 给定一个链表,移除从列表尾部开始的第n个结点并返回它的头结点 举例: 给定链表 1 -> 2 -> 3 -> 4 -> 5,且n = 2 移除倒数第二个结点后就变成了 1 ...

Olivia_he 220

PVE虚拟机运行不流畅,出现卡顿。

🏆本文收录于《优快云问答解惑-专业版》专栏,主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!

**My Coding Family** 3064

3.2 直流无刷电机(BLDC):状态空间模型与传递函数

状态空间模型和传递函数是分析BLDC电机动态特性的重要工具。状态空间模型通过微分方程描述系统内部状态与输入输出关系,适用于多变量系统分析;传递函数则在频域表征系统特性,便于控制器设计。本文详细推导了三相坐标系和dq坐标系下的BLDC电机状态空间模型,讨论了线性化方法,并介绍了从状态空间模型到传递函数的转换过程。这些模型为BLDC电机的控制系统设计提供了理论基础。

技术工作者 245

leetcode题解第19题 Remove Nth Node From End of List(删除链表的倒数第N个节点)

考查列表操作的一道题,题目大意如下: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。 样例输入: head = 1->2->3->4->5 n = 2 样例输出: 1->2->3->5 题目链接:https://leetcode.com/problems/remove-nth-node-from-end-of-list/ e...

笔墨留年。 541

RT-DETR改进策略【卷积层】| CGblock 内容引导网络 利用不同层次信息,提高多类别分类能力 (含二次创新)

本文记录的是利用中的模块优化的目标检测网络模型。通过局部特征提取器、周围环境提取器、联合特征提取器和全局环境提取器来提取局部特征、周围环境和全局环境信息,充分利用不同层次的信息。本文将其应用到中,并进行二次创新,使网络能够更好的处理多类别目标的分类能力。CGNet: A Light-Weight Context Guided Network for Semantic Segmentation1️⃣ 首先通过局部特征提取器floc(∗)floc(*)floc(∗)和周围环境提取器fsur(∗)f_{sur }

Limiiiing的博客 1350

LeetCode 19. Remove Nth Node From End of List

LeetCode 19. Remove Nth Node From End of ListDescriptionExampleNoteCodeConclusion Description Given a linked list, remove the n-th node from the end of list and return its head. Example Note Given n ...

gq__97的博客 304

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

19. Remove Nth Node From End of List

题目:Remove Nth Node From End of List 移除list中倒数第n个节点 难度:中等 Given a linked list, remove then-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5,...

qq_21963133的博客 241

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Remove Nth Node From End of List

Remove Nth Node From End of List 文章目录Remove Nth Node From End of List题目分析代码如下 题目 Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2...

执剑者罗辑的博客 284

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Leetcode #19. Remove Nth Node From End of List 移除倒数第N个链表节点 解题小节

1 题目理解我昨天又忘了更新了。。~~(>_<)~~ 先补这道题就是说,删除链表的倒数第N个节点。。 链表本身是不知道有多少个元素的。。不过这种题都一个解法 设置Faster和slower两个指针,faster指针比slower先走k-1步。。然后再一起移动。。这样faster到头时,slower也指向了倒数第k个了~~~然后就标准的移除了。。很简单。。不多说,取快递去了2 原题Remove N

MebiuW的专栏 1005

LeetCode 19. Remove Nth Node From End of List 对撞指针

Remove Nth Node From End of List 题目 题意 注意 思路 代码 结果 Delete Node in a Linked List 题目 题意 注意 思路 代码 结果19.Remove Nth Node From End of List题目 Given a linked list, remove the nth node from the end of list an

老夫编代码就是一把梭 423

算法系列——删除链表的倒数第 N 个结点(Remove Nth Node From End of List)

Given a linked list, remove the nth node from the end of list and return its head

BridgeGeorge 637

【LeetCode 面试经典150题】19. Remove Nth Node From End of List 删除链表倒数第N个节点

即从链表末尾移除第 n 个节点。方法使用双指针(快慢指针)技术,能够在一次遍历中完成任务。给定一个链表的头节点,移除链表末尾的第 n 个节点,并返回链表的头节点。你能否只遍历一次完成此任务?

qq_43513394的博客 525

LeetCode 19. Remove Nth Node From End of List(链表)

题目来源:https://leetcode.com/problems/remove-nth-node-from-end-of-list/submissions/ 问题描述 19.Remove Nth Node From End of List Medium Given a linked list, remove then-th node from the end of list and...

da_kao_la的博客 146

[leet code] Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the

伪技回忆录 5246

19. Remove Nth Node From End of List从链表结尾删除第n个节点Python

给定一个链表,从列表末尾删除第n个节点并返回其头。 Input:1->2->3->4->5, n=2 Output:1->2->3->5 Method 1 将链表转为list删掉指定位置数字之后, 转为链表输出 1. 将链表中的数字存入list中 {l=[] ; while head: l.append(head.val) head=head.next} 2.删除倒数第n个数字然后将list中的...

weixin_44516745的博客 440

leetcode:19. Remove Nth Node From End of List

Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the l...

jmhIcoding 408

[LeetCode 19] Remove Nth Node From End of List 移除链表倒数第N个节点

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, th...

alan711的博客 186

LeetCode Remove Nth Node From End of List

将一个链表中的倒数第n个元素从链表中去除。

DRFish 1312
上一篇: Leetcode NO.21 Merge Two Sorted Lists && 合并链表(有序)
下一篇: Leetcode NO.255 Implement Stack Using Queues 使用队列实现栈
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值