LeetCode #19 - Remove Nth Node From End of List

搭建ipv6并发代理池 我开了个知识星球,把将本期搭建写成shell脚本已放到了里面,有需要的小伙伴可以自行去取,jd的参数,阿里bxet纯算、快手滑块,Pdd—Anti,ikuai,b站login之-极验3文字,某音ab,瑞数456vmp补环境等等,主要是某些大佬加我问成品,很多人都打着白嫖的手段去加我好友为目的,问完基本就以后没有任何的交集,这样的交好友雀氏没有任何意义。不如我直接开个星球,里面直接放我逆向的成品以及逆向的思路,这样大家各取所需,我也有点收益,意义更明确点,免得浪费大家的时间。我是pve下的ubuntu。 阅读详情

题目描述:

 

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.

 

一次遍历即可删除倒数第n个节点,可以采用两个指针的方法,第一个指针p1比第二个指针p2早n步出发,当p1到达链表最后一个元素时,即p1->next=NULL,第二个指针到达倒数第n+1个节点,此时只要将p2->next=p2->next->next即可除去倒数第n个节点。但是需要注意的是,n可以为链表节点的总数,此时p1会指向NULL,如果还要判断if(p1->next==NULL)就会出错,所以应该先行判断,当p1=NULL时,说明除去第一个节点,返回head->next。

 

 

/**
 * 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* p1=head;
        ListNode* p2=head;
        for(int i=0;i<n;i++)
            p1=p1->next;
        if(p1==NULL) return head->next;
        while(p1->next!=NULL)
        {
            p1=p1->next;
            p2=p2->next;
        }
        p2->next=p2->next->next;
        return head;
    }
};

 

 

 

 

 

苹果生态最佳搭档:Mac mini + OpenClaw + iPhone 远程控制 本文详细介绍如何通过iPhone远程控制Mac mini上的OpenClaw应用,打造高效苹果生态工作流。内容包括:1) 苹果生态整合优势,包括AirDrop、Handoff等功能对比;2) Mac mini配置步骤,涵盖SSH/VNC设置、网络优化及Docker部署OpenClaw;3) iPhone端SSH/VNC客户端选择与Tailscale内网穿透配置;4) 详细SSH密钥设置与连接测试方法。通过这套方案,用户可实现跨设备无缝控制OpenClaw应用,提升远程工作效率。(149字) 阅读详情

相关推荐

字节开源 DeerFlow 2.0:从 Deep Research 到 Super Agent Harness 的进化之路

GitHub Trending 冠军项目,字节跳动完全开源的超级智能体框架。支持子代理、沙箱执行、长期记忆、技能扩展,可完成从几分钟到几小时的复杂任务。本文深度解析架构设计、安装实战、技能系统,并附上独家配置指南。

qq_38610923的博客 1708

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

[STM32]HAL STM32F103 软件I2C读写MPU6050(附DMP库移植)

本文介绍了如何在STM32F103C8T6上通过I2C协议与MPU6050传感器通信并实现DMP功能。首先,使用STM32CubeMX配置项目,设置时钟和I2C接口。然后,编写软件I2C协议的读写函数与MPU6050交互。接着,移植DMP库并进行必要修改,获取加速度计和陀螺仪数据,并将其转换为欧拉角显示在OLED上。最后,利用Keil的逻辑分析仪监测I2C时序。

m0_56841583的博客 2321

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

胸口好想碎大石 810

《LangChain技术原理剖析:OpenAI Function Calling的实践与应用》

,"unit": {},name:函数的唯一标识符:自然语言描述,决定LLM如何选择该函数parameters:定义输入参数的完整Schema。

qq_62223405的博客 980

Android 集成 Google 与 Facebook 第三方登录实战详解(2024版)

本文详细介绍了2024年Android应用集成Google与Facebook第三方登录的完整实战指南。通过分步教程和代码示例,帮助开发者快速实现一键登录功能,提升用户体验和注册转化率,同时涵盖隐私合规和常见错误处理的最佳实践。

blue的专栏 822

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. 问题描述:删除链表倒数第n个节点。 设置新的head,统计链表长度,当删除第一个节点时需要额外处理,否则当链表长度为1时返回的head并未经过处理。 class Solution { public: ListNode * ...

thebs2的博客 126

从ORCAP-1589警告看Cadence ORCAD的DRC设置:如何避免‘网络别名’引发的潜在短路风险

本文深入解析Cadence ORCAD中ORCAP-1589警告的深层含义,探讨网络别名可能引发的短路风险及DRC设置优化方案。通过分析网络别名的设计意图、DRC规则配置及最佳实践,帮助工程师有效规避设计风险,提升电路设计的可靠性和规范性。

weixin_34194829的博客 297

LeetCode #19 Remove Nth Node From End of List

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

菜鸟程序员的成长历程 403

Win11下高效管理JDK1.8与JDK17双环境的实战指南

本文提供了一份在Windows 11系统上高效管理JDK 1.8与JDK 17双环境的实战指南。文章详细讲解了如何实现双版本切换,包括如何干净安装两个JDK、通过巧妙配置环境变量实现命令行一键切换,以及在不同IDE和构建工具中为项目独立配置JDK版本,旨在帮助Java开发者彻底解决版本冲突问题,提升开发效率。

ipfs8storage的博客 542

Cesium 实现气象(温度,降水,风速)可视化详细代码实现

本教程介绍了使用Cesium实现气象数据可视化的方法,主要包括:1) 搭建Cesium开发环境;2) 三种核心气象要素的展示技术:温度热力图(基于颜色渐变和点密度)、降水区域填充(矩形着色)和风速风向(粒子系统);3) 数据集成方案,包括模拟数据生成和API对接;4) 高级功能如3D温度柱和时间序列动画;5) 性能优化策略,如数据聚合和LOD控制。教程提供了完整的代码示例,从基础配置到视觉效果实现,帮助开发者构建交互式气象可视化应用。

专注AI大模型,软件混淆,授权 1843

leetcode个人题解——#19 Remove Nth Node From End of List

思路:设置两个指针,其中第二个指针比第一个延迟n个元素,这样,当第二个指针遍历到指针尾部时,对第一个指针进行删除操作。 当然,这题要注意一些边界值,比如输入[1,2] n=2时如果按照思路走会指向未分配的内存空间,要加一个条件判断。 /** * Definition for singly-linked list. * struct ListNode { * int va...

weixin_30360497的博客 92

【从零到一】Jetson Nano上YOLOv5部署完全攻略:零基础小白也能玩转边缘AI推理

文章摘要: 本文详细介绍了在Jetson Nano边缘设备上部署YOLOv5目标检测模型的完整流程。从硬件准备、系统镜像烧录、基础环境配置,到CUDA/cuDNN验证、Python虚拟环境搭建及PyTorch安装,逐步指导开发者完成部署准备。教程兼顾理论原理与实践操作,提供Python和C++两种实现方案,并涵盖TensorRT优化、实时检测等生产级应用场景,帮助零基础开发者快速掌握边缘AI推理技术,实现高性能目标检测应用。

LXY_LOVE36的博客 367

[Leetcode]#19 Remove Nth Node From End of List

//#19 Remove Nth Node From End of List //4ms 100% #include <iostream> using namespace std; /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * L

jerseyma的博客 277

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【一天一道LeetCode】#19. Remove Nth Node From End of List

一天一道LeetCode系列(一)题目 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

ZeeCoder 568

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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

zcy19941015的博客 302

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

[Leetcode] #19 Remove Nth Node From End of List

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

奔跑的小乌龟 266

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

题意:删除一个单向链表的倒数第 N 个节点 直接模拟,先算出节点数,再找到节点删除 加一个头结点dummy,并使用双指针p1和p2。p1先向前移动n个节点,然后p1和p2同时移动,当p1.next==None时,此时p2.next指的就是需要删除的节点。 class Solution: # @return a ListNode def removeNthFromEnd(sel

小草莓的博客 185

leetCode #19 Remove Nth Node From End of List

题目:去掉单链表中倒数第n个结点 分析:要只走一趟,用2个指针一起走。 答案: class Solution { public: ListNode *removeNthFromEnd(ListNode *head, int n) { if (head == NULL) return NULL; List

MarStarck的专栏 428

Remove Nth Node From End of List_Leetcode_#19

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

LittleBob180的专栏 301

[LeetCode] #19 Remove Nth Node From End of List

Given a linked list, remove thenthnode 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...

weixin_30586257的博客 67

[LeetCode]#19 Remove Nth Node From the End of list

从移动回来一个月,就一直在忙项目申请书的事情。通过写申请书,看新闻联播的新闻稿都开始顺眼了。 师兄师姐们已经开始找工作了,我还有一年时间,工作方面早作准备。机器学习方面继续推导常见的算法,刷一刷kaggle,做做特征工程什么的,算是应用了。 今天这道题其实并不难,但我是在Linux下写的,有些本地调试的方法和技巧想记录一下。 一、题目 Given a linked list, rem...

weixin_30278311的博客 79

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

allforwhat的专栏 466

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 (E)

[Problem] Given a linked list, remove thenthnode 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...

weixin_30594001的博客 56

LeetCode_#19_Remove_Nth_Node_From_End_of_List

class Solution { public: ListNode * removeNthFromEnd(ListNode* head, int n) { ListNode* ptr = head; int len = 1; while (ptr->next != NULL) { len++; ptr = ptr->next; } ListNode*...

真的想嫁给朱一龙诶 165
上一篇: LeetCode #10 - Regular Expression Matching
下一篇: LeetCode #21 - Merge Two Sorted Lists
LawFile
博客等级 码龄10年 5粉丝 627原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值