leetcode--19 Remove Nth Node From End of List

锐捷交换机路由器配置全攻略:从基础命令到高级功能(附常用命令速查表) 本文提供锐捷交换机路由器配置全攻略,涵盖从基础命令到高级功能的详细指南,包括VLAN划分、DHCP配置、链路聚合等实用技巧,并附常用命令速查表,帮助网络管理员快速掌握锐捷设备运维。 阅读详情

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.

单链表的题,简单。
陷阱:没有传统的head指针。head指针直接指向第一个元素。
采用最直接的思路,首先遍历,算出链表长度,然后删除倒数第n个数。
时间为4ms。
代码如下:

/**
 * 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) {
        struct ListNode* trueHead=(struct ListNode*)malloc(sizeof(struct ListNode));//注意指针类型的转换
        trueHead->next = head;
        struct ListNode* pointPtr= trueHead->next;
        int len=0;
        while( pointPtr )//遍历求长度
        {
            ++len;
            pointPtr = pointPtr->next;
        }
        pointPtr= trueHead;
        for(int i=0; i < len-n; ++i)
        {
            pointPtr = pointPtr->next;
        }
        pointPtr->next = pointPtr->next->next;
        head = trueHead->next;
        return head;
    }
};

查看其它高人的代码,发现一些匪夷所思的方法,特转载到此处,借鉴学习。
地址:https://leetcode.com/discuss/36163/4ms-c-solution

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

地址:https://leetcode.com/discuss/37464/c-7-lines-solutions

ListNode *fast = head, **cur = &head;
    while(n--) fast = fast->next;
    while (fast) {
        fast = fast->next;
        cur = &(*cur)->next;  }
    *cur = (*cur)->next;
    return head;
程序员应用AI大模型创业实现财务自由的100种赚钱方案原理与实战指南 成功的 AI 创业者都遵循着 “技术 + 需求 + 变现” 的铁三角法则。记住:不是要做 “全能大模型”,而是成为某个垂直领域的 “AI 解决方案专家”。现在就开始行动:用周末 2 小时完成第一个 MVP,下周启动种子用户测试,三个月内找到可复制的盈利模式。当你实现第一个 10 万用户时,会发现财务自由的密码藏在持续迭代的每个 0.1 版本更新中。延伸思考:你的技术背景适合切入哪个细分领域? 阅读详情

相关推荐

Quantum Espresso实战进阶:HSE混合泛函在能带计算中的精准应用

本文深入探讨了在Quantum Espresso中应用HSE混合泛函进行高精度能带计算的实战方法。针对传统DFT方法低估半导体带隙的问题,HSE通过混合精确交换能,显著提升计算精度。文章详细讲解了从输入文件配置、参数设置(如nqx网格、exxdiv_treatment)、到自洽与非自洽计算流程,以及结果可视化与对比的全过程,并提供了关键的调优与避坑指南,助力实现从PBE到HSE的精准进阶。

weixin_28363123的博客 236

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

RK3588S Andriod12调试OLED屏幕背光

4,kernel-5.10/drivers/gpu/drm/drm_mipi_dsi.c中mipi_dsi_dcs_set_display_brightness函数就是调节亮度的地方。这一步很重要,因为上层调节背光时就是操作的这个背光设备节点(/sys/class/backlight/backlight),如果名字不对,上层调用的通路就没有打通。之后在dcs_bl_update_status函数中做个映射,通过调整brightness_table中的值就可以对背光亮度的调节曲线作出调整。

u011774634的博客 4444

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

Unity3D 集成 SQLite 数据库实战:从零搭建到数据可视化

本文详细介绍了在Unity3D项目中集成SQLite数据库的完整实战流程。从SQLite的轻量、稳定、易用特性讲起,逐步指导开发者完成环境配置、驱动集成、C#脚本编写(涵盖连接、建表、增删改查),并进阶演示了数据模型封装与在Unity UI中实现数据可视化的方法。最后提供了多线程访问、路径差异、性能优化等关键避坑指南,帮助开发者构建可靠高效的本地数据管理方案。

weixin_29291069的博客 106

Part03 数据结构

本文介绍了CSP-S初赛中常见的高级数据结构及其实现,包括: 线性结构:双端栈、双端队列、单调队列、优先队列和ST表,重点分析了它们的特点、时间复杂度及典型应用场景。 集合与森林:并查集(带路径压缩和按秩合并)和树的孩子兄弟表示法,展示了多叉树转化为二叉树存储的方法。 特殊树结构:包括二叉堆、树状数组、线段树和字典树,详细说明了它们的构建、查询和更新操作,特别强调了线段树的递归实现和字典树的前缀匹配特性。 每种数据结构都配有核心C++代码实现,涉及数组模拟、动态规划和树形结构等编程技巧,适合算法竞赛选手快速

weixin_43863957的博客 1186

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

军品研制过程评审活动-(一)论证阶段

军品研制过程评审活动-(一)论证阶段

Abel的博客 3824

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

实战5:农作物产量预测 蔬菜产量预测 lstm 完整代码数据-可直接运行-计算机毕设

实战5:农作物产量预测 蔬菜产量预测 lstm 完整代码数据-可直接运行-计算机毕设

机器学习深度学习业余选手 1262

宇树G1语音助手完整开发指南(上)——从零构建智能知识库对话系统

本文将详细介绍如何为宇树G1人形机器人开发一个,实现"唤醒 → 对话 → 查询 → 回复"的智能助手功能。

m0_47999117的博客 803

LeetCode 19. Remove Nth Node From End of List 题解

本题是链表的经典问题,主要考察对链表操作和双指针技巧的理解和应用。通过使用双指针,我们可以高效地删除链表的倒数第 N 个节点。双指针的核心思想是:先让快指针移动 N+1 步,然后同时移动快慢指针,当快指针到达链表末尾时,慢指针恰好指向倒数第 N+1 个节点,此时可以方便地删除倒数第 N 个节点。这种方法不仅适用于删除链表的倒数第 N 个节点问题,还可以应用于许多其他需要定位链表中特定位置节点的场景。掌握双指针技巧,对于解决链表问题非常重要。

cannonjinx的博客 4630

OpenClaw 安装失败:npm install 多次报错的完整排错与解决方案

这次问题的最终解决,其实并不复杂,但关键在于思路的转变:从“脚本怎么又失败了”,切换为“npm 在这一层到底失败在哪里”。当我们手动复现安装过程、打开 verbose 日志、直面真实错误栈时,问题就不再是模糊的“安装不了”,而是一个可以被定位、被验证、被修复的具体工程问题。

博客 1万+

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

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

【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. Example: Given linked list: 1->2->3->4->5, and n = 2. Aft...

zj20165149的博客 234

IDEA的使用指南

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

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

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

LeetCode 19 Remove Nth Node From End of List 题解

LeetCode 19 Remove Nth Node From End of List题意思路方法1 指针数组方法2 当前倒数第n个节点代码 题意 给一个链表,要求删除链表中的倒数第n个节点并返回链表的首节点,数据保证n有效。要求一次遍历得到结果。 思路 方法1 指针数组 将所有的节点指针值存到数组中,遍历之后通过数组直接访问倒数第n个指针,不过这种方法显然有点不太好。 方法2 当前倒数第n个节...

EricJeffrey的博客 170

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

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

LeetCode (力扣) 19. Remove Nth Node From End of List (C) - Medium

同步发于JuzerTech网站,里面有我软、硬件学习的纪录与科技产品开箱,欢迎进去观看。

JuzerTech的博客 165

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

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

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

Made in Code 781

[LeetCode] 19. Remove Nth Node From End of List Java

题目: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...

AC616288149的博客 286

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

LeetCode 19. Remove Nth Node From End of List

题目描述(中等难度) 给定一个链表,将倒数第 n 个结点删除。 解法一 删除一个结点,无非是遍历链表找到那个结点前边的结点,然后改变下指向就好了。但由于它是链表,它的长度我们并不知道,我们得先遍历一遍得到它的长度,之后用长度减去 n 就是要删除的结点的位置,然后遍历到结点的前一个位置就好了。 public ListNode removeNthFromEnd(ListNode head, int ...

wind_liang的博客 920

【Java】【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 e...

zhelong19830726的博客 382
上一篇: leetcode-62&63 Unique Paths I & II
下一篇: leetcode-20 Valid Parentheses
evolone
博客等级 码龄14年 399粉丝 98原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值