19. Remove Nth Node From End of List

离线安装samba与配置(.tar方式安装) 个人记录 阅读详情
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 linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.

解法一,双指针大法:

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head == NULL || n <= 0)
            return head;

        ListNode* fast = head;
        for(int i=0; i<n && fast!=NULL; ++i)
            fast = fast->next;

        if(fast == NULL)
            return head->next;

        ListNode* slow = head;
        while(fast->next != NULL){
            slow = slow->next;
            fast = fast->next;
        }

        slow->next = slow->next->next;

        return head;
    }
};

解法二,stack+二级指针:

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head == NULL || n <= 0)
            return head;

        ListNode** pp = &head;
        std::stack<ListNode**> st;
        while(*pp != NULL){
            st.push(pp);
            pp = &((*pp)->next);
        }

        ListNode** target = NULL;
        int count = 0;
        while(!st.empty()){
            target = st.top();
            st.pop();
            if(++count == n)
                break;
        }

        *target = (*target)->next;

        return head;
    }
};

解法二可以通过测试用例,但是解法二没有对N的长度是否大于链表长度进行判断,不过如果N的长度=链表长度,解法二而*target=(*target)->next会满足要求删除了头部。

解法一和二实际上都没有处理链表长度K远远小于N的情况,实际上这种情况直接返回head就可以了。

所以解法一最完整的写法应该是:

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head == NULL || n <= 0)
            return head;

        ListNode* fast = head;
        int i;
        for(i=0; i<n && fast!=NULL; ++i)
            fast = fast->next;

        if(fast == NULL){
            if(i == n)
                return head->next;
            else
                return head;
        }

        ListNode* slow = head;
        while(fast->next != NULL){
            slow = slow->next;
            fast = fast->next;
        }

        ListNode* tmp = slow->next;
        slow->next = slow->next->next;
        delete tmp;   //加上delete

        return head;
    }
};

解法二就不写了。

深入解析PVT corner:从wcl到ml的芯片设计关键考量 本文深入解析了芯片设计中的PVT corner概念,重点探讨了从传统WC到先进工艺下WCL、ML等关键工艺角。文章详细阐述了温度反转效应如何使低温WCL成为最严苛的时序场景,并提供了基于先进工艺的实战签核策略与多角多模分析流程,确保芯片在极端工艺、电压、温度下的稳健性。 阅读详情

相关推荐

IVP 2021 UNETR: Transformers for 3D Medical Image Segmentation

动机 具有跟踪和扩展路径的全卷积神经网络(FCNNs)(如编码器和解码器),近年来在各种医学图像分割应用中显示出了突出的地位。在这些体系结构中,编码器通过学习全局上下文表示来扮演一个不可或缺的角色,该全局上下文表示将进一步用于解码器的语义输出预测。 卷积层作为FCNN的主要构造块,其局部性限制了此类网络学习长时空间依赖的能力。 在NLP领域,基于transformer的模型已经在各种任务中实现了最先进的基准。transformer中的自我注意机制使他们能够动态地突出词序列的重要特征并学习其长时依赖关系。这

smile909的博客 1979

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/ 题目描述 Given a linked list, remove the n-th node from the end of list and return its head. Example:...

珍妮的选择的博客 252

合成孔径雷达(SAR)的常见工作模式

随着星载SAR技术研究的深入发展,单一的条带模式已经无法满足当今时代的需求,因此许多新的工作模式相继被提出。这些新模式在分辨率和测绘宽度等方面都有所提高,接下来将简要介绍几种常用的工作模式。

m0_51390495的博客 2318

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

LoRa项目实战:手把手教你为ESP32选配和焊接天线(从PCB到信号测试)

本文详细介绍了为ESP32选配和焊接LoRa天线的全流程,包括天线选型、PCB布局设计、焊接工艺及信号测试。重点解析了PCB板载天线、导线天线和陶瓷贴片天线的适用场景与性能特点,并提供专业的焊接技巧和信号测试方案,帮助开发者优化LoRa模块的无线通信性能。

weixin_30470643的博客 396

ArcGIS小白必看:如何用3D Analyst工具将二维点线秒变三维模型(附详细步骤)

本文为ArcGIS初学者提供了利用3D Analyst工具将二维点、线数据快速转换为三维模型的完整指南。详细讲解了基于属性字段和数字高程模型(DEM)两种核心方法,并附上从数据准备、工具操作到三维场景可视化与问题排查的详细步骤,帮助用户轻松实现从平面到立体的空间数据建模。

nft7creator的博客 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

旺崽的博客 2449

Oracle登录报错ORA-12560?5分钟搞定TNS协议适配器错误的完整修复指南

本文详细解析了Oracle登录报错ORA-12560(TNS协议适配器错误)的成因与解决方案。从基础服务检查到高级网络配置排查,提供了一套完整的修复流程,帮助数据库管理员快速解决连接问题,确保Oracle数据库的稳定运行。

weixin_29278805的博客 524

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

OpenFOAM-多孔介质模型(porous media)

文章目录一、多孔介质(porous media)二、使用步骤1.达西定律2.Darcy-Forchheimer3.OpenFOAM fvOptions4.paraview动画每帧显示时间(Annotate Time)5.参考内容总结 一、多孔介质(porous media) 在流体力学中,流体流经多孔介质是指流体流经多孔介质时的行为方式,如海绵或木材,或使用沙子或其他多孔材料过滤水。 我们将展示如何设置多孔层,并使用OpenFOAM模拟流体通过该介质。 二、使用步骤 1.达西定律 达西定律最初是由达西通.

zq93538196的博客 9939

Python 高级语法:闭包

闭包(Closure)是函数式编程中的一个重要概念。在Python中,闭包可以简单理解为:一种特殊的函数。外函数:嵌套函数中最外层的函数称之为外函数。内函数:嵌套函数中最内层的函数称之为内函数;内函数引用了外函数的临时变量;外函数的返回使用了临时变量的内函数。

moment159的博客 2355

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

飞牛fnNAS存储模式RAID 5数据恢复

中介绍了fnNAS存储空间的几个模式,细心的网友应该能感受到,我是非常推崇RAID 5的。但我认为,一般人出现1块硬盘损坏后,就能够赶紧采取处理措施了,再高的容灾等级也无法满足这个用户的需求了。》就完成了飞牛NAS存储空间的说明,包括了如何创建、怎么选择空间模式、RAID 5 硬盘故障后的更换、系统更换/重装后数据盘的恢复等等。在VMware中新建虚拟机,安装好fnNAS后,再向虚拟机添加“已有硬盘”(这就是所谓崩溃虚拟机的数据盘),此时新的fnNAS出现提示。在“选择要添加的硬盘”中,选择刚添加硬盘。

dgnankai的专栏 3012

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. After removing the second node from the end, the linke

繁城落叶 1091

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

LeetCode //C - 19. Remove Nth Node From End of List

【代码】LeetCode //C - 19. Remove Nth Node From End of List。

Made in Code 780

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

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

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

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 a linked list, remove the nth node from the end of list and return its head. For exam

littledouble的专栏 455

[C语言][LeetCode][19]Remove Nth Node From End of List

题目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 n

Timsley的专栏 1585

19. Remove Nth Node From End of List [easy] (Python)

题目链接https://leetcode.com/problems/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

Coder_Orz的博客 3006

19 Remove Nth Node From End of list

给定链表,删除倒数第n个链表节点 输入:链表的头节点,n 输出:链表的头节点 要求:一次提交通过 边界: n = 1 index=size-1 n = size index = 0 思路:最简单的方法为,先遍历一遍,求出来总长度,根据总长度求出需要删除的节点位置,然后遍历到位置,进行删除,返回。 时间复杂度还是O(size) 减小复杂度只能通过不进行第二遍遍历。用...

cxhttt的博客 152

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_34023863的博客 71
上一篇: 92. Reverse Linked List II(链表局部逆序**)
下一篇: 147. Insertion Sort List(插入排序)
FreeeLinux
博客等级 码龄11年 421粉丝 356原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值