要求

  • 给定一个链表,删除倒数第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. 全网最值得推荐的ELKB日志学习博客-博客地址留存

    博客地址:https://elasticstack.blog.csdn.net/article/details/102728604 博客地址留存,后续解决疑难问题

  2. A. 1.划分数列

    A . 1. 划 分 数 列 A. 1.划分数列 A.1.划分数列 氵水沝淼㵘解析 先预处理,然后公式推(详见代码) Code #include <bits/stdc++.h> using ...

  3. java面试-什么是GC root

    一.什么是垃圾 内存中已经不再被使用到的空间就是垃圾 二.要进行垃圾回收,如何判断一个对象是否可以被回收? 引用计数法 很难解决对象之间的循环引用问题 枚举根节点做可达性分析 通过一系列名为" ...

  4. Nginx的进程管理与重载原理

    目录 进程结构图 信号量管理 Linux的信号量管理机制 利用信号量管理Nginx进程 配置文件重载原理 进程结构图 Nginx是多进程结构,多进程结构设计是为了保证Nginx的高可用高可靠,包含: ...

  5. SimpleDateFormat一定是线程不安全吗?

    今天一位优秀的架构师告诉我,下面这段代码SimpleDateFormat是线程不安全的. /** * 将Date按格式转化成String * * @param date Date对象 * @param ...

  6. 基本dos命令

    Dos命令 打开cmd方法 开始---windows系统---命令提示符 win键 + R键 输入cmd --- 回车 按住Shift键---右击任意文件夹-----单击在此处打开PowerShell ...

  7. Day07_35_Colection下的方法

    Collection 下的方法 * **Collection 集合的方法应用** ``` package com.shige.Collection; import java.util.ArrayLis ...

  8. python对BP神经网络实现

    python对BP神经网络实现 一.概念理解 开始之前首先了解一下BP神经网络,BP的英文是back propagationd的意思,它是一种按误差反向传播(简称误差反传)训练的多层前馈网络,其算法称 ...

  9. JDK8新特性(一) Lambda表达式及相关特性

    函数式接口 函数式接口是1.8中的新特性,他不属于新语法,更像是一种规范 面向对象接口复习 在这里先回顾一下面向对象的接口,创建接口的关键字为interface,这里创建一个日志接口: public ...

  10. hdu4284 dfs+floyd

    题意:       给你n个城市,m条边,要有h个必须旅游和打工的城市,问你能不能从1把所有必须的h个城市全部旅游并且打工完... 思路:       先一遍floyd跑出全局最短路,然后暴力枚举出打 ...