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. TF随笔-12

    #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Tue Aug 1 08:14:30 2017 ...

  2. Python itertools.combinations 和 itertools.permutations 等价代码实现

    最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...

  3. .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换

    .NET/C# 程序从 Main 函数开始执行,基本上各种书籍资料都是这么写的.不过,我们可以写多个 Main 函数,然后在项目文件中设置应该选择哪一个 Main 函数. 你可能会觉得这样没有什么用, ...

  4. asp.net core microservices 架构之分布式自动计算(三)-kafka日志同步至elasticsearch和kibana展示

    一 kafka consumer准备 前面的章节进行了分布式job的自动计算的概念讲解以及实践.上次分布式日志说过日志写进kafka,是需要进行处理,以便合理的进行展示,分布式日志的量和我们对日志的重 ...

  5. DesignPattern(四)结构型模式(下)

    上篇链接  https://www.cnblogs.com/qixinbo/p/9023764.html 继续介绍最后三种结构型模式 外观模式 外观模式,也称作 ”门面“模式,在系统中,客户端经常需要 ...

  6. Java与WCF交互(一):Java客户端调用WCF服务 【转】

    原文:http://www.cnblogs.com/downmoon/archive/2010/08/24/1807161.html 最近开始了解WCF,写了个最简单的Helloworld,想通过ja ...

  7. 图解VS2005之单元测试

    据说VS2005里即提供了测试功能,可是对于像我或者我们这样的开发人或团队真还没有进化到用测试这块.一直以来都是手工测试或等到用户发现问题.今天在网上找了一个介绍单元测试的WORD文档,按里面说的做了 ...

  8. 【转】UBUNTU 下GIT的安装

    原文网址:http://www.cnblogs.com/perseus/archive/2012/01/06/2314069.html linux下软件的安装方式有多种,最简单的莫过于从软件中心直接安 ...

  9. 4.Appium实现自动化安装apk

    一.代码如下所示: from appium import webdriver import os apk_path = os.path.abspath(os.path.join(os.path.dir ...

  10. iis 在站点中新建虚拟目录站点之后,虚拟目录中的 web.config 与 主站点中的 web.config冲突解决方案

    在虚拟目录站点中增加如下配置即可:<clear/>