要求

  • 给定一个链表,删除倒数第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. SSL证书详解和CFSSL工具使用

    公钥基础设施(PKI) 基础概念 CA(Certification Authority)证书,指的是权威机构给我们颁发的证书. 密钥就是用来加解密用的文件或者字符串.密钥在非对称加密的领域里,指的是私 ...

  2. vue-cli2 项目中使用node-sass

    公司的项目,换了个电脑要重新安装一下依赖,但是直接npm install的时候报错了,提示node-sass未安装成功. 然后直接npn install node-sass --save 的时候还是下 ...

  3. 浏览ASP.NET网页(6)

    当我们搭建好了IIS后,就不需要开发工具进行编译打开网站啦,我们可以在IIS下进行预览,如图所示: 需要注意的是,网页的后缀名是.aspx,不是.cs

  4. 【spring源码系列】之【xml解析】

    1. 读源码的方法 java程序员都知道读源码的重要性,尤其是spring的源码,代码设计不仅优雅,而且功能越来越强大,几乎可以与很多开源框架整合,让应用更易于专注业务领域开发.但是能把spring的 ...

  5. Box UVA - 1587

    Ivan works at a factory that produces heavy machinery. He has a simple job - he knocks up wooden box ...

  6. Day13_67_interrupt() 方法

    interrupt() 方法 中断线程 * interrupt()方法的简单理解 - interrupt() 方法只是改变线程的阻塞状态而已,让一个正在阻塞状态的线程,恢复执行.但是它不会中断一个正在 ...

  7. 幻读:听说有人认为我是被MVCC干掉的

    @ 目录 前言 系列文章 一.我是谁? 二.为什么有人会认为我是被MVCC干掉的 三.我真的是被MVCC解决的? 四.再聊当前读.快照读 当前读 快照读 五.告诉你们吧!当前读的情况下我是被next- ...

  8. 1.Java开发环境搭建

    Java开发环境搭建 date: 2021-4-7 19:17:30 JDK安装 下载所需的JDK版本,点此下载JDK8的安装包 挑选对应的系统版本 配置环境变量 打开高级系统设置,找到系统变量 在系 ...

  9. 2.1.3- 体会css样式

    css初始 css样式规则 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  10. 【主从复制】MySQL主从复制的原理

    1. 存在几个线程: 主库一个线程,从库两个线程 2.主库生成一个log dump线程,和从库IO线程交互 3.IO线程请求主库binlog,写入到中继日志relay log 4.SQL线程读取中继日 ...