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

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

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

思路:双指针,相差n

/**
* 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) {
ListNode* pFirst = head, *pSecond = head;
for(int i = ; i < n; i++){
pFirst = pFirst->next;
}
if(pFirst == NULL){
return head->next;
}
while(pFirst->next){
pFirst = pFirst->next;
pSecond = pSecond->next;
}
pSecond->next = pSecond->next->next;
return head;
}
};

19.Remove Nth Node From End of List(List; Two-Pointers)的更多相关文章

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

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

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

  3. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

  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个节点

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

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

  7. [leetcode 19] Remove Nth Node From End of List

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

  8. Java [leetcode 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 ...

  9. Leetcode 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 ...

  10. 【一天一道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 ...

随机推荐

  1. 【转】Linux中文件的可读,可写,可执行权限的解读以及chmod,chown,chgrp命令的用法

    chmod是更改文件的权限 chown是改改文件的属主与属组 chgrp只是更改文件的属组. 一.文件权限解读 如上图所示,开头的-rwxrw-r--这一字符串标识文件权限. 这个字符串有10位,可以 ...

  2. Ubuntu下MySQL数据库文件 物理迁移后 出现的问题

    参考资料: https://www.cnblogs.com/advocate/archive/2013/11/19/3431606.html 本文要解决的一个问题是数据库文件进行物理迁移时遇到的问题. ...

  3. v4l2框架函数调用关系

    所有的设备节点和子设备节点都是通过__video_register_device()注册的 1.对于video设备节点: 用户空间ioctl(VIDIOC_S_FMT)---> v4l2_fop ...

  4. nginx-vod-module && docker && docker-compose 测试

    nginx-vod-module appdemo i reference [nginx-vod-module-docker](https://github.com/NYTimes/nginx-vod- ...

  5. 初识安卓小程序(Android短信发送器)

    首先,先创建一个安卓项目(我的版本号是4.4.2的),名字为"短信发送器" 然后在res目录下找到layout目录,找到activity_main.xml或fragment_mai ...

  6. 对ashx请求用Gzip,Deflated压缩

    //GZIP压缩 //查看请求头部 string acceptEncoding = context.Request.Headers["Accept-Encoding"].ToStr ...

  7. 根据tomcat的日志判断web的发布路径以及服务路径

    [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.HostConfig.unde ...

  8. CCFLOW5 SDK 模式 开发环境配置

    在群里和论坛里问了N次都没有人回答,最终在QQ好友[冥(276669806) ]的帮助下成功配置了SDK开发环境.现将具体配置步骤分享给大家.1.打开VS2010 新建一个网站项目2.将CCFlow\ ...

  9. 记录tomcat的完整日志

    Tomcat报的错太含糊了,什么错都没报出来,只提示了Error listenerStart.为了调试,我们要获得更详细的日志.可以在WEB-INF/classes目录下新建一个文件叫logging. ...

  10. 【linux】linux DD命令

    Linux-dd命令详解 dd 是 Linux/UNIX 下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 例1:要把一张软盘的内容拷贝到另一张软盘上,利用/t ...