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.

Hide Tags

Linked List Two Pointers

 

  简单的快慢指针问题。
#include <iostream>
using namespace std; /**
* 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==NULL) return NULL;
ListNode * fastp = head, * slowp = head;
for(int i =;i<n;i++)
fastp = fastp->next;
if(fastp==NULL) return head->next;
while(fastp->next!=NULL){
fastp = fastp ->next;
slowp = slowp ->next;
}
slowp->next = slowp->next->next;
return head;
}
}; int main()
{
return ;
}

[LeetCode] Remove Nth Node From End of List 快慢指针的更多相关文章

  1. LeetCode: Remove Nth Node From End of List 解题报告

    Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...

  2. [LeetCode] 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 ...

  3. [leetcode]Remove Nth Node From End of List @ Python

    原题地址:http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ 题意: Given a linked list, remo ...

  4. LeetCode——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 ...

  5. Leetcode 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] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

  7. leetcode remove Nth Node from End python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  8. LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

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

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

随机推荐

  1. Nginx+proxy_cache图片缓存

    搭建图片缓存机制的原理在于减少数据库的负担并加快静态资源的响应. 步骤: 1. vim /usr/local/nginx/conf/nginx.conf 2. http{     ...     .. ...

  2. thinkphp 3.2.3 - Route.class.php 解析(路由匹配)

    class Route { public static function check(){ $depr = C('URL_PATHINFO_DEPR'); // '/' $regx = preg_re ...

  3. django开发傻瓜教程-1-安装和HelloWorld

    安装 sudo pip install Django 新建项目 django-admin startproject XXX 启动项目 进入主目录下 python manage.py runserver ...

  4. codeforces 258D DP

    D. Little Elephant and Broken Sorting time limit per test 2 seconds memory limit per test 256 megaby ...

  5. HDU 6053 ( TrickGCD ) 分块+容斥

    TrickGCD Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  6. Robot Framework Webdriver For Firefox FQA

    记录一下过程中使用的问题,希望大家碰到类似问题能够提高效率解决. 问题1.通过js脚本定位unieap框架网页中radio选项. 通过执行js脚本获取radio选项,并通过xpath路径点击. js脚 ...

  7. django 开发之模型以及静态问题和图片的使用

    使用Django的模型,基本步骤: 1.创建model 2.加入到admin.py中去 3.执行生成迁移:python manage.py makemigrations blog 4.执行迁移,生成表 ...

  8. MySQL基础2-创建表和主键约束

    1.创建表 在操作数据表之前,应该使用"USE 数据库名"指定操作是在哪个数据库中进行 主键约束(唯一标识) ****非空*** ****唯一*** ****被引用****(学习外 ...

  9. leetcode 【 Subsets 】python 实现

    题目: Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset mus ...

  10. centos开机启动项设置命令:chkconfig

    在CentOS或者RedHat其他系统下,如果是后面安装的服务,如httpd.mysqld.postfix等,安装后系统默认不会自动启动的.就算手动执行/etc/init.d/mysqld start ...