要求

  • 给定一个链表,删除倒数第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. DDOS攻击与防御简单阐述,列出DDOS的攻击方法和防御方法

    参考1:https://www.hi-linux.com/posts/50873.html#%E7%BD%91%E7%BB%9C%E5%B1%82-ddos-%E6%94%BB%E5%87%BB 什么 ...

  2. 2020牛客NOIP赛前集训营-普及组(第二场)A-面试

    面 试 面试 面试 题目描述 牛牛内推了好多人去牛客网参加面试,面试总共分四轮,每轮的面试官都会对面试者的发挥进行评分.评分有 A B C D 四种.如果面试者在四轮中有一次发挥被评为 D,或者两次发 ...

  3. docker部署skywalking

    https://www.cnblogs.com/xiao987334176/p/13530575.html

  4. 初学 Babel 工作原理

    前言 Babel 对于前端开发者来说应该是很熟悉了,日常开发中基本上是离不开它的. 已经 9102 了,我们已经能够熟练地使用 es2015+ 的语法.但是对于浏览器来说,可能和它们还不够熟悉,我们得 ...

  5. 如何讲一个网页转换为jpg?(图片!)

    不需要安装插件!!! 打开网页,打开开发者工具 快捷键: ctrl+shift+p输入>full即可自动下载!

  6. 【笔记】《Redis设计与实现》chapter9 数据库

    9.1 服务器中的数据库 Redis服务器将所有都保存在服务器状态redis.h/redisServer结构中 struct redisServer{ //... // 一个数组,保存着服务器中所有数 ...

  7. IP Networks UVA - 1590

     Alex is administrator of IP networks. His clients have a bunch of individual IP addresses and he de ...

  8. 进击中的Vue 3——“电动车电池范围计算器”开源项目

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文参考:https://dzone.com/articles/build-a-tesla-battery- ...

  9. boltdb的实现和改进

    整个代码不是很复杂,可以从代码中理解如何实现. 特点:btree,很小巧,但实现了完整事务机制,稳定,即使丢电也不会导致数据库错误. 整个结构如下: meta page (前两页) --- > ...

  10. 【Feign/Ribbon】记录一次生产上的SpringCloudFeign的重试问题

    在上周在的微供有数项目中(数据产品),需要对接企业微信中第三方应用,在使用Feign的去调用微服务的用户模块用微信的code获取access_token以及用户工厂信息时出现Feign重试超时报错的情 ...