要求

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

示例

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

边界

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

思路

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

实现

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

    1.Eureka概述 1.1.什么是Eureka Eureka是Netflix的一个子模块.基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移. 只需要使用服务的标识符,就可以访问到 ...

  2. 使用pr进行视频的剪辑

    本人作为一个经常浪迹在各个小视频的网站的视频界"gai溜子",很是喜欢一些人上传的综艺或者电影的搞笑搞笑小桥段,有的剪辑下来的片段甚至会比我们自己去看还要有意思的多.我认为视频剪辑 ...

  3. 【面试技巧】老生常谈之 n 种使用 CSS 实现三角形的技巧

    在一些面经中,经常能看到有关 CSS 的题目都会有一道如何使用 CSS 绘制三角形,而常见的回答通常也只有使用 border 进行绘制一种方法. 而 CSS 发展到今天,其实有很多有意思的仅仅使用 C ...

  4. 动图:删除链表的倒数第 N 个结点

    本文主要介绍一道面试中常考链表删除相关的题目,即 leetcode 19. 删除链表的倒数第 N 个结点.采用 双指针 + 动图 的方式进行剖析,供大家参考,希望对大家有所帮组. 19. 删除链表的倒 ...

  5. (十六)Struts2的标签库

    一.简介 Struts2的标签库使用OGNL为基础,大大简化了数据的输出,也提供了大量标签来生成页面效果,功能非常强大. 在早期的web应用开发中,jsp页面主要使用jsp脚本来控制输出.jsp页面嵌 ...

  6. MySQL实战45讲,丁奇带你搞懂

    之前,你大概都是通过搜索别人的经验来解决问题.如果能够理解MySQL的工作原理,那么在遇到问题的时候,是不是就能更快地直戳问题的本质? 以实战中的常见问题为切入点,带你剖析现象背后的本质原因.为你串起 ...

  7. 因为这几个TypeScript代码的坏习惯,同事被罚了500块

    作者:Daniel Bartholomae 翻译:疯狂的技术宅 原文链接:https://startup-cto.net/10-bad-typescript-habits-to-break-this- ...

  8. hibernate的三种查询方式

    hibernate的三种查询方式 目录 hibernate的三种查询方式 1.HQL查询 1.1.SQL概述 1.2.实体查询 1.3.带where的查询 1.3.属性查询 1.4.实体的更新和删除 ...

  9. 华为AppTouch创新订阅模式,出海创收事半功倍

    去年是中国游戏在全球舞台大放异彩的一年:在国内游戏市场竞争愈发激烈的情况下,不少厂商将目光投向更为广阔的海外市场,把海外视为新的增长点.<PUBG Mobile><万国觉醒>等 ...

  10. 基于excel的接口自动化测试框架:支持参数化、关联等

    1. 框架结构说明 2. 框架代码实现 action 包  case_action.py business_process 包 main_process.py util 包 global_var.py ...