【LeetCode】 Remove Nth Node From End of List 【python C++】做法详解

DFX从芯片到系统:可制造性、可测试性与可靠性设计实战指南 DFX(Design for eXcellence)是一套面向产品全生命周期的设计方法论,涵盖可制造性(DFM)、可测试性(DFT)、可靠性(DFR)等维度。其核心原理是将制造、测试、维护等环节的约束前置到设计阶段,从而降低流片与量产风险。在芯片与系统硬件领域,DFX不仅是提升良率与测试效率的关键手段,更是保障车规、工控等严苛场景下长期稳定运行的基础。从芯片物理实现到PCB布局,再到系统级散热与维护设计,DFX的落地需要工具链、流程闸门与跨部门协作的体系化支撑。本文基于实际项目经验,分享DFX从设计决策到工 阅读详情

题目,从listnode 中删除倒数第n个点

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.

解答:

1、首先我们需要找到倒数第n,正着数是第几个

2、我们用cur和pre一起指向head节点,把cur节点往前走N个,那么cur剩下的长度就是我们所需要的长度

3、把cur走空,pre跟着cur一起走,当cur走到NULL的时候,pre正好走到了需要删除的那个点的前一个

4、把pre->next=pre->next->next 就可以了

5、处理当n=list的长度-1时的情况,在第2步后面增加一个判断,如果cur为空,那么head=head->next;

代码:

#C++ 版本
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* pre=head;
        ListNode* cur=head;
        for(int i=0;i<n;i++){
            cur=cur->next;
        }
        if(cur==NULL){
            head=head->next;
            return head;
        }
        while(cur->next){
            pre=pre->next;
            cur=cur->next;
        }
        pre->next=pre->next->next;
        return head;
    }
};

#Python 版本
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        head2,head3=head,head
        for i in range(n):
            head2=head2.next
        if not head2:
            return head.next
        while head2.next:
            head3=head3.next
            head2=head2.next
        head3.next=head3.next.next
        return head

 

naca翼型_STARCCM+计算二维翼型气动性能 STAR-CCM+计算二维翼型气动性能一算例背景机翼理论主要研究翼型在流体中运动时的力学特性。在工程领域中,机翼以升力面、控制面、叶片或桨叶等形式出现。舰船上的舵、水翼、减摇鳍等都是机翼,螺旋桨、汽轮机叶片和压缩机叶片也都是利用机翼原理工作的,而在研究船舶操纵性时,甚至还可把船体的水下部分看作一个巨大的机翼。随着航空科学的发展,世界各主要航空发达的国家建立了各种翼型系列。美国有NACA系... 阅读详情

相关推荐

九齐NY8A051F低功耗模式全解析:从待机到休眠的省电实战

本文全面解析台湾九齐NY8A051F MCU的低功耗模式,从待机到休眠的省电实战技巧。通过实测数据展示四种工作模式的电流消耗差异,提供模式切换的标准流程和唤醒时序优化策略,并分享外围电路省电设计和低电压检测的实战应用,帮助开发者实现物联网设备的超长续航。

weixin_30839881的博客 407

LeetCode *19. Remove Nth Node From End of List (Python Solution)

题目描述 Given a linked list, remove the n-th node from the end of list and return its head. 给定一个链表,从列表末尾删除第n个节点并返回其头指针。 Example 1: Input: 1 Output: true Explanation: 20 = 1 Example 2: Input: 16 Output...

X’s BLOG 194

Android TV开发:RecyclerView焦点控制实践与优化

在Android TV应用开发中,焦点控制是提升用户体验的关键技术。不同于移动端触摸交互,TV端通过遥控器方向键导航,需要精确的焦点管理机制。RecyclerView作为动态列表的核心组件,其焦点控制涉及视图复用、导航逻辑和视觉反馈等核心技术点。通过合理配置focusable属性、自定义焦点查找算法以及优化动画性能,开发者可以解决TV端常见的焦点跳转异常、视觉反馈延迟等问题。特别是在视频点播、智能家居控制等场景中,良好的焦点控制能显著提升操作效率。本文结合网格布局焦点环绕、分页加载焦点保持等实战案例,深入解

weixin_34195142的博客 468

python 单向链表查找倒数第n个元素_移除链表倒数第n个元素

移除链表倒数第n个元素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.Note:Given n will always be valid.Try to do this in...

weixin_39781209的博客 168

《探秘 BME680温度湿度气压气体传感器模块:环境感知的卓越之选》:此文为AI自动生成

BME680 是一款由博世公司推出的集成环境传感器模块,在环境监测和物联网等领域中发挥着重要作用。它能够同时测量温度、湿度、气压和气体浓度等多个环境参数,为各种应用提供准确可靠的环境数据。该模块采用小巧的封装形式,便于集成到各种设备中,无论是智能家居、可穿戴设备还是工业监测系统,都能轻松适配。例如,在智能家居系统中,BME680 模块可以监测室内环境参数,如温度、湿度、气压和空气质量等。通过与智能家居控制器或智能手机应用程序的连接,用户可以实时了解室内环境状况,并根据需要进行调整。

zheng_ruiguo的专栏 1865

紫光同创PGL50H开发板实战:盘古50KH视频板的多媒体处理与高速通信方案

本文详细介绍了基于紫光同创PGL50H FPGA芯片的盘古50KH视频开发板在多媒体处理与高速通信方面的实战应用。文章从开发板硬件架构解析入手,逐步指导开发环境搭建、DDR3内存控制器配置、HDMI视频输入输出实战,并重点探讨了利用板载HSST高速收发器实现光纤通信与万兆以太网的方案。最后,通过构建一个集多路视频采集、处理与光纤传输于一体的系统案例,展示了该开发板在高性能实时数据处理领域的强大潜力。

b9c0d的博客 484

leetcode 019 删除链表的倒数第N个节点(Remove Nth Node From End of List ) python 最简代码,快慢指针

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

Jasmine_dream 214

【脑电信号处理与特征提取】P2-夏晓磊:脑电的神经起源与测量

ElectroEncephaloGraphyTherefore, EEG produces a graphed representation of the electrical activity occurring in a person’s brain.(把一个人的大脑中的电活动画出来)脑电头皮表面记录到的大脑活动产生的电信号(微伏)。(记录位置:硬脑膜/大脑皮层表面- ECoG,神经元群- LFP与MUR/MUA:活动位置:心电、眼电、肌电等)脑电图电压随时间变化的波形图。

m0_51474171的博客 1893

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 =...

咸鱼夏の翻身观察记录 284

Cadence OrCAD Capture 保持元器件编号情况复制元件,复制后元器件编号与复制前不同的问题解决

如下图所示复制时,已经勾选了“Preserve reference on copy”但是复制后的编号却与复制前不一样。本文简单介绍其原因及解决方法。

fydar的博客 3163

复现报告:《多回路人机控制系统仿真及II型PIO分析》方法复现与代码实现

本文复现了谭文倩硕士论文《多回路人机控制系统仿真及II型PIO分析》中的关键方法,重点实现了频域驾驶员模型辨识和参数灵敏度分析。通过数字化提取论文图表数据,构建了结构驾驶员模型传递函数,并验证了参数变化对系统特性的影响。采用MATLAB实现了描述函数计算、模型参数辨识和灵敏度分析算法,为后续神经网络模型辨识和II型PIO研究奠定了基础。由于缺乏原始实验数据,部分结果通过数值模拟获得,但仍有效重现了论文的核心分析方法。~~~~~

huanghm88的专栏 175

LeetCode 19 — Remove Nth Node From End of List(C++ Java Python)

题目:http://oj.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->2

龙之梦 3145

外骨骼机器人(二):自适应振荡器

振荡器是一种数学工具,可以用于实时跟踪、预测类周期信号的频率、相位、幅值等,而助行外骨骼机器人需要自动适应人体的行走频率、幅度,两者相结合,就能使助行外骨骼机器人拥有更舒适、更自然的行走步态。 ......

行者无疆的博客 4262

[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)

[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)

水果君の日常 3094

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

【力扣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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Leetcode19. Remove Nth Node From End of List

Leetcode19. 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. Aft...

magic_jiayu的博客 221

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

[leetcode] 19. Remove Nth Node From End of List

Description 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. 删除链表的倒数第 N 个结点的Python和C++代码实现。

Learning from the mistakes 202

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 linked

给永远比拿愉快 1096

[Leetcode 19, Easy] Remove Nth Node From End of List

Leetcode, easy

算法的实践 557

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, t...

dldldl1994的博客 153

[LeetCode Python3/C++]19. Remove Nth Node From End of List

Python3 solution: class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: if not head: return head result = ListNode(0) result.next, cur = head, result while n: head = h

xvrixingkong的博客 102

leetcode_[python/C++]_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, the li

Mosen 561

【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

【LeetCode】19. Remove Nth Node From End of List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/...

负雪明烛 3077

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-&gt;2-&gt;3-&gt;4-&gt;5, and n = 2. After removing the second node from the end...

qq_36183810的博客 160

[LeetCode19]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

coder 进阶的专栏 810

LeetCode19. Remove Nth Node From End of List(C++/Python)

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 end, t...

qq_41562704的博客 203
上一篇: 【LeetCode】 4Sum 【Python C++】
下一篇: matplotlib jupyter notebook 图像可视化 plt show
Jie Ou
博客等级 码龄10年 176粉丝 111原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值