LeetCode 之 Remove Nth Node From End of List

maxwell 平面变压器仿真(一) 在学习开关电源设计的时候,我们难免要设计变压器,采用有限元仿真软件MAXWELL 可以帮助我们确定变压器的励磁电感,漏感等参数。对于我们的理论分析大有裨益。但是苦于这破软件的教程真的是相当的零散,我相信大家用起来也总是苦于没有系统的教程指导,撰写本文的目的有两个,一个是记录自己使用的一些问题,一个是给大家提供一个可以参考的文档。 阅读详情

题目链接:Remove Nth Node From End of List
这道题的意思是从链表的尾部开始查找,删除第n个节点。
最普通的方法就是遍历两次,第一次遍历计算出链表的长度为len,那么倒数第n个节点,即为正数第len-n+1个,第二次遍历删除对应节点即可。
本以为这样的做法不过AC,但是竟然通过了。

解法一:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head==null)
            return head;
        ListNode index = new ListNode(0);
        index.next = head;
        int len = 1;
        while(head.next!=null){
            len++;
            head = head.next;
        }
        int tmp = len-n+1;
        head = index.next;
        if(tmp == 1){
            head = head.next;
            return head;
        }
        while(tmp>2){
            head = head.next;
            tmp--;
        }
        head.next = head.next.next;

        return index.next;

    }
}

解法二:只需要遍历一次,使用双指针的方法,faster和slower,初始状态faster和slower均指向head节点,首先faster向前走n步,因为n始终为有效的即其不会超过链表的长度,如果此时faster==null,则说明n==len,即删除倒数第len个节点也就是正数第一个节点,那么返回head.next即可。
如果faster!=null,那么接下来faster和slower同时向前走,直到faster走到链表的尾部,而此时slower指向的即为将要删除的节点的前一个节点。

代码如下:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head==null||head.next==null)
            return null;
        ListNode faster = head;
        ListNode slower = head;

        int i = 0;
        while(i<n){
            faster = faster.next;
            i++;       
        }

        if(faster==null)
            return head.next;

        while(faster.next!=null){
            slower = slower.next;
            faster = faster.next;
        }
        slower.next = slower.next.next;
        return head;
    }
}
Mac版Burp Suite专业版破解与汉化完整指南 在Web安全测试领域,Java应用程序的许可证验证机制与本地化实现是常见的技术课题。其原理通常涉及Java Agent技术,通过在JVM启动时动态修改字节码,实现对软件授权状态的校验逻辑进行拦截与重写,从而绕过官方验证。同时,软件界面的国际化(i18n)与本地化(l10n)则依赖于资源文件的替换与加载。从技术价值角度看,深入理解这些机制不仅能满足特定场景下的工具定制需求,更能加深对Java安全机制、软件保护与逆向工程的理解。在实际的工程实践中,安全研究人员与开发者常面临功能受限或语言障碍的挑战,尤其是在渗透 阅读详情

相关推荐

Windows下快速搭建Kettle开发环境:基于9.4.0.0版本的保姆级教程

本文提供Windows系统下Kettle 9.4.0.0开发环境的详细搭建指南,涵盖源码获取、Maven配置优化、高效编译及部署运行全流程。通过实战验证的步骤和常见问题解决方案,帮助数据工程师快速完成环境配置,提升ETL开发效率。

weixin_28829479的博客 331

Remove Nth Node From End of List 解题报告

Remove Nth Node From End of ListDescriptionGiven a linked list, remove the nth node from the end of list and return its head.NoticeThe minimum number of nodes in list is n.ExampleGiven linked list: 1->

总结沉淀 654

用友U8接口开发全攻略:从EAI到OpenAPI的5种方式详解(附避坑指南)

本文深度解析了用友U8系统接口开发的五种核心方式:EAI、API、OpenAPI、SQL直连与内部CO组件。文章详细对比了各方式的适用场景、优缺点及实战操作,并提供了清晰的选型决策框架与避坑指南,帮助开发者根据具体业务需求选择最高效、最稳定的集成路径。

weixin_29322855的博客 684

Remove Nth Node From End of List--LeetCode

1.题目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 removi

涛爸的博客 267

Chatbox使用指南

Chatbox使用指南

weixin_65289420的博客 2万+

从零开始:用YOLOv8-Pose实现实时人体姿态检测(附完整代码)

本文提供了一份从零开始的完整实战指南,详细介绍了如何使用YOLOv8-Pose构建实时人体姿态检测系统。内容涵盖环境搭建、数据准备、模型训练与调优,以及高性能部署和进阶应用开发,并附有可直接运行的代码示例,帮助开发者快速掌握Pose Estimation技术并将其应用于实际项目。

h6i7j8的博客 621

LeetCode 第 19 题 (Remove Nth Node From End of List)

LeetCode 第 19 题 (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.

Ivan 的专栏 2311

ARM的CI-700和Arteris的NoC对比

设计理念:CI-700注重于为移动和客户端SoC提供定制化的互连解决方案,而Arteris的NoC更侧重于提供灵活、可配置的网络互连技术,适用于多种应用场景。性能与功耗:CI-700通过系统级缓存和网状拓扑结构优化性能和功耗,而Arteris的NoC通过物理感知和多协议支持来提升性能和降低功耗。功能安全:Arteris的NoC提供了功能安全支持,这对于汽车和工业应用尤为重要,而CI-700虽然也关注安全性,但未明确提及功能安全支持。互操作性。

ygyglg的专栏 2622

【力扣Leetcode题解系列之0019—Remove Nth Node From End of List 删除链表的倒数第 N 个结点】

本文介绍了删除链表倒数第N个节点的常见解法——双指针法,并提供了Python、Java、C、C++和Go五种语言的实现代码。该算法通过引入哑节点和快慢指针,在O(L)时间复杂度和O(1)空间复杂度内解决问题,其中L为链表长度。关键点在于哑节点的使用确保删除操作的一致性,以及指针同步移动的准确性。文章还分析了算法复杂性和实现难点,并提出了两个扩展题目:删除倒数第k到m个节点和删除特定值的倒数第n个节点,为读者提供了进一步的思考方向。

youngerwang的博客 1431

别再只盯着CXL协议了,聊聊CXL.mem里那个能动态调优内存性能的‘QoS遥测’到底怎么用

本文深入解析CXL.mem协议中的QoS遥测功能,探讨如何通过DevLoad指标动态调优内存性能。文章详细介绍了QoS遥测的核心机制、多QoS类别设备的实战配置,以及AI训练、金融交易和视频处理场景中的优化案例,帮助数据中心实现高效内存资源管理。

weixin_33725722的博客 436

QT应用编程: Win10系统下QT5.12搭建Android开发环境(配件齐全)

在win10系统下,搭建QT5.12.6 的Android开发环境。

7688

Leetcode1-100: 19. Remove Nth Node From End of List

Leetcode1-100: 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

Y123iwantit的博客 183

全新RT-DETR 有效涨点改进目录 | 包含各种卷积、主干改进、各种注意力机制、Neck特征融合改进、损失函数、AIFI创新改进、独家创新、小目标检测、特殊场景检测等最全大论文及小论文必备创新改进点

《全新最全RT-DETR涨点改进专栏》提供500+种创新改进方案,涵盖卷积、注意力机制、主干网络等模块优化,适用于PCB检测、工业缺陷检测等场景。专栏包含顶会论文改进点、数据集增强方法、热力图可视化等实用功能,帮助科研人员快速提升模型性能。提供完整环境配置指南(Python3.10+Pytorch2.5.1)和训练教程,支持不同版本RT-DETR模型。定期更新涨点策略,配套QQ答疑群免费指导,适合从入门到发论文全流程需求,特别推荐给目标检测领域的研究生和工程师。

Ai缝合怪、助力小伙伴高效发论文 3404

Leetcode 19. Remove Nth Node From End of List

https://leetcode.com/problems/remove-nth-node-from-end-of-list/ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), n...

weixin_33762321的博客 89

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

LeetCode [链表] 19.Remove Nth Node From End of List (C++和Python实现)

19.Remove Nth Node From End of List [难度:中等] 【题目】 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, and n =...

咸鱼夏の翻身观察记录 285

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Leetcode之Remove Nth Node From End of List 问题

问题描述: Given a linked list, remove the nth node from the end of list (倒数第n个结点)and return its head. Note: Given n will always be valid. Try to do this in one pass. 示例: Given linked list: 1->2->3->

漫漫学习路 369

Leetcode学习笔记:#19. Remove Nth Node From End of List

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. 实现: public ListNode removeNthFromEnd(ListNode head, int n){ List...

ccystewart的博客 143

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

[LeetCode] Remove Nth Node From End of List

[Problem] 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 en

1.02^365的成长裂变 3608

leetcode 19. Remove Nth Node From End of List

一 题目 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, and n = 2. After removing the second node from the ...

bohu83的博客 388

LeetCode Remove Nth Node From End of List

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

DRFish 1312

LeetCode——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

三分地 1786

【LeetCode】Remove Nth Node From End of List

DescriptionRemove Nth Node From End of List Given a linked list, remove the nth node from the end of list and return its head.

VegB的博客 330

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 linked list becomes 1->2->3->5. Note: Given n wi

weixin_44610959的博客 390

leetcode Remove Nth Node From End of List

题目连接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ Remove Nth Node From End of List Description Given a linked list, remove the $n^{th}$ node from the end of list and return...

dong12276的博客 176

leetcode--19. Remove Nth Node From End of List

题目:19. Remove Nth Node From End of List 链接:https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 大概意思是要删除单链表的倒数第N个结点,要求只能遍历一遍。那就先设置一个指针从前往后遍历到第N个结点,然后再设置一个指针从头开始同步遍历,第一个指针遍历到链尾

Nero Zhang的博客 291

leetcode 19 Remove Nth Node From End of List

Remove Nth Node From End of List Total Accepted: 74559 Total Submissions: 274808 Difficulty: Easy Given a linked list, remove the nth node from the end of list and return its head. For exa

javays1的专栏 348
上一篇: LeetCode 之 Container With Most Water
下一篇: LeetCode 之 Divide Two Integers
小菜鸟要高飞
博客等级 码龄12年 14粉丝 77原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值