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. json数据格式及json格式化工具推荐

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于编程语言的文本格式来存储和表示数据. 易于人阅读和编写,同时也易于机器解析和生成. XML也 ...

  2. JZOJ 2136. 【GDKOI2004】汉诺塔

    2136. [GDKOI2004]汉诺塔 (Standard IO) Time Limits: 3000 ms  Memory Limits: 128000 KB  Detailed Limits   ...

  3. Android系统中标准Intent的使用

    Android系统用于Activity的标准Intent 1.根据联系人ID显示联系人信息= Intent intent=new Intent(); intent.setAction(Intent.A ...

  4. python3下最全的wordcloud用法,附源代码及相关文件

    一.wordcloud是什么 词云,在一段文本中提取关键词进行扁平化的展示,更能吸引目标客户的眼球. 市面上有很多在线生成词云的工具,本文以Python中的第三方库wordcloud为例讲解如何自动生 ...

  5. DFS:BZOJ1085-骑士精神

    题目: 1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1461  Solved: 796 [Submit][ ...

  6. CART树 python小样例

    决策树不断将数据切分成小数据集,直到所有目标变量完全相同,或者数据不能再切分为止,决策时是一种贪心算法,它要在给定的时间内做出最佳选择,但并不关心能否达到最优 树回归 优点:可以对复杂和非线性的数据建 ...

  7. js 实现5秒倒计时后跳转页面

    <script type="text/javascript"> function countDown(secs, surl) { var jumpTo = docume ...

  8. js中123==123为false的问题--写成123=="123"即可解决问题

    项目中遇到过一个问题,js拿到后台返回的一个数字,在if中判断时,出现类似123==123为false的结果, 初步分析原因,应该是返回的是string类型的,但拿来比较的是个number类型的,所以 ...

  9. loj2058 「TJOI / HEOI2016」求和

    推柿子 第二类斯特林数的容斥表达 fft卡精度就用ntt吧qwq. #include <iostream> #include <cstdio> using namespace ...

  10. Python框架之Django学习笔记(十六)

    Django框架之表单(续) 今天简直无力吐槽了,去了香山,结果和网上看到的简直是天壤之别啊,说好的香山的枫树呢?说好的香山的红叶呢?说好的漫山遍野一片红呢?本以为在山上,一口气爬上去,沿路基本都是翠 ...