要求

  • 给定一个链表,删除倒数第n个节点

示例

  • 1->2->3->4->5->NULL , n=2
  • 1->2->3->5

边界

  • n是从0还是从1计
  • n不合法,负数或者大于链表长度如何处理

思路

  • 遍历一遍计算链表长度,再遍历一遍删除倒数第n个节点
  • 使用两个指针同时移动,找到待删除节点的前一个节点

实现

 1 struct ListNode {
2 int val;
3 ListNode *next;
4 ListNode(int x) : val(x), next(NULL) {}
5 };
6
7 class Solution {
8 public:
9 ListNode* removeNthFromEnd(ListNode* head, int n) {
10
11 assert( n>=0 );
12 ListNode* dummyHead = new ListNode(0);
13 dummyHead->next = head;
14
15 ListNode* p = dummyHead;
16 ListNode* q = dummyHead;
17 for( int i = 0 ; i < n + 1 ; i ++ ){
18 assert( q );
19 q = q->next;
20 }
21
22 while( q != NULL){
23 p = p->next;
24 q = q->next;
25 }
26
27 ListNode* delNode = p->next;
28 p->next = delNode->next;
29 delete delNode;
30
31 ListNode* retNode = dummyHead->next;
32 delete dummyHead;
33
34 return retNode;
35 }
36 };

相关

  • 61 Rotate List
  • 143 Reorder List
  • 234 Palindrome Linked List

[刷题] 19 Remove Nth Node From End of List的更多相关文章

  1. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

  2. 61. Rotate List(M);19. Remove Nth Node From End of List(M)

    61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...

  3. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  4. 【LeetCode】19. Remove Nth Node From End of List (2 solutions)

    Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

  6. (链表 双指针) 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 l ...

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

  8. [LeetCode] 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, Give ...

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

随机推荐

  1. 手机浏览器通过Scheme跳转APP,兼容各种手机浏览器

    一个比较完整的产品线,必定有APP和网站,另外还有微信公众号网页和小程序.那么有一个比较常见的需求就是在手机浏览器内打开APP,实现起来也比较简单,只要APP配置的有URLScheme即可. 但是因为 ...

  2. 图像Resize方式对深度学习模型效果的影响

    在基于卷积神经网络的应用过程中,图像Resize是必不可少的一个步骤.通常原始图像尺寸比较大,比如常见监控摄像机出来的是1080P高清或者720P准高清画面,而网络模型输入一般没有这么大,像Yolo系 ...

  3. JavaWeb 补充(XML)

    XML 1. 概念:Extensible Markup Language 可扩展标记语言 可扩展:标签都是自定义的. <user>  <student> 功能: 存储数据   ...

  4. CentOS系统安装Nginx

    目录 1. 官网下载地址 2. 上传到服务器安装 2.1 检查是否安装以下软件包 2.2 安装 2.3 安装nginx 3. 启动&停止 nginx是 HTTP 和反向代理服务器,邮件(IMA ...

  5. Day12_62_线程的生命周期

    线程的生命周期 要实现多线程,必须在主线程中创建新的线程对象. 任何线程一般都具有五种状态,即创建,就绪,运行,阻塞,终止(消亡) 新建状态:在程序中创建了一个新的线程对象后,新的线程对象便处于新建状 ...

  6. H5 hybrid开发-前端资源本地化方案纪要

    H5 hybrid-前端资源本地化方案纪要 就整个行业来说,大前端是趋势,现阶段,native方面除了一些偏CPU密集型工作与操作系统底层API方面的工作外,H5基本都可以满足需要. 目前的工作更偏向 ...

  7. NumPy之:结构化数组详解

    目录 简介 结构化数组中的字段field 结构化数据类型 创建结构化数据类型 从元组创建 从逗号分割的dtype创建 从字典创建 操作结构化数据类型 Offsets 和Alignment Field ...

  8. 【docker-compose】docker-compose环境安装

    docker-compose: 是一个用于定义和运行多容器 Docker 的应用程序工具,可以帮助我们可以轻松.高效的管理容器 安装: 1.安装pip 工具-目的是为了下载docker-compose ...

  9. 指定pdf的格式

    爬虫实战[3]Python-如何将html转化为pdf(PdfKit)   前言 前面我们对博客园的文章进行了爬取,结果比较令人满意,可以一下子下载某个博主的所有文章了.但是,我们获取的只有文章中的文 ...

  10. 书评第001篇:《C++黑客编程揭秘与防范》

    本书基本信息 作者:冀云(编著) 出版社:人民邮电出版社 出版时间:2012-6-1 ISBN:9787115280640 版次:1 页数:265 字数:406000 印刷时间:2012-6-1 开本 ...