题目链接

  题目要求:

  Given a linked list, remove the nth node from the end of list and return its head.

  For example,

   Given linked list: ->->->->, and n = .

   After removing the second node from the end, the linked list becomes ->->->.

  Note:

  Given n will always be valid.
  Try to do this in one pass.

  这道题比较常规的想法是先算出链表长度len,再将头指针移动len-n步就可以到达待删节点了。具体程序如下(4ms):

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(!head)
return head; int len = ;
ListNode *start = head;
while(start)
{
len++;
start = start->next;
} if(len == n)
{
start = head;
head = head->next;
delete start;
start = nullptr;
return head;
} ListNode *preNode = head;
start = preNode->next;
int k = ;
while(k < len - n)
{
preNode = start;
start = start->next;
k++;
} preNode->next = start->next;
delete start;
start = nullptr; return head;
}
};

  另一个比较巧妙的方法就是先定义一个慢指针(初始值为头指针父指针),让头指针移动n步,再让头指针和一个慢指针同时移动直至头指针为空,这时慢指针指向的下一个节点就是待删节点。具体程序日如下(4ms):

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(!head)
return head; ListNode *dummy = new(nothrow) ListNode(INT_MIN);
assert(dummy);
dummy->next = head; for(int i = ; i < n; i++)
head = head->next; ListNode *preNode = dummy;
while(head)
{
head = head->next;
preNode = preNode->next;
} ListNode *delNode = preNode->next;
preNode->next = delNode->next;
delete delNode;
delNode = nullptr; head = dummy->next;
delete dummy;
dummy = nullptr; return head;
}
};

LeetCode之“链表”:Remove Nth Node From End of List的更多相关文章

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

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

  2. 【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 ...

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

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

  4. 【一天一道LeetCode】#19. Remove Nth Node From End of List

    一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...

  5. LeetCode OJ 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, Give ...

  6. 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  ...

  7. 【LeetCode OJ】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: G ...

  8. LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  9. 【LeetCode】019. 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, Give ...

  10. LeetCode:19. Remove Nth Node From End of List(Medium)

    1. 原题链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2. 题目要求 给出一个链表,请 ...

随机推荐

  1. iOS使用自签名证书实现HTTPS请求

    概述 在16年的WWDC中,Apple已表示将从2017年1月1日起,所有新提交的App必须强制性应用HTTPS协议来进行网络请求. 默认情况下非HTTPS的网络访问是禁止的并且不能再通过简单粗暴的向 ...

  2. 联想G510F1F2..功能键和FN+功能键反过来

    进入BIOS, 将HotKey Mode 修改为Disabled,右边有详细说明:

  3. cassandra 监控方案评估

    摘要 最开始做cassandra monitor 方案的选型时,主要是从cassandra 本身入手,后来发现cassandra运行在JVM上,所有的metrics都是通过JMX 暴露出来.所以又可以 ...

  4. Java进阶(四十三)线程与进程的区别

    Java进阶(四十三)线程与进程的区别 1.线程的基本概念   概念:线程是进程中执行运算的最小单位,是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点在运行中必 ...

  5. listener.ora--sqlnet.ora--tnsnames.ora的关系以及手工配置举例(转载:http://blog.chinaunix.net/uid-83572-id-5510.ht)

    listener.ora--sqlnet.ora--tnsnames.ora的关系以及手工配置举例 ====================最近看到好多人说到tns或者数据库不能登录等问题,就索性总结 ...

  6. SQLite Update 语句(http://www.w3cschool.cc/sqlite/sqlite-update.html)

    SQLite Update 语句 SQLite 的 UPDATE 查询用于修改表中已有的记录.可以使用带有 WHERE 子句的 UPDATE 查询来更新选定行,否则所有的行都会被更新. 语法 带有 W ...

  7. Android Multimedia框架总结(六)C++中MediaPlayer的C/S架构

    转载请把头部出处链接和尾部二维码一起转载,本文出自: http://blog.csdn.net/hejjunlin/article/details/52435789 前面几节中,都是通过java层调用 ...

  8. XMPP(一)-openfire服务端的安装和搭建

    XMPP全称:可扩展通讯和表示协议 简介:可扩展通讯和表示协议 (XMPP) 可用于服务类实时通讯.表示和需求响应服务中的XML数据元流式传输.XMPP以Jabber协议为基础,而Jabber是即时通 ...

  9. Linux--DNS服务器

     DNS是Internet上使用最普遍,也是最重要的服务之一,通过DNS我们才可以访 问丰富多彩的网络,而DNS服务器就是为了实现域名解析功能而搭建的. 域名系统采用层次结构,按地理区域或机构区域 ...

  10. java操作XML文件--读取内容

          先把问题贴出来:编写一个可以解析xml及修改xml内容的工具类       由于我以前做过Android应用程序开发,之前也解析过xml文件,所以,这道题不是很难,这篇文章我先解决第一个问 ...