Given a linked list, remove the n th 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个结点的前驱即可,然后连接其前驱和后继即可,值得注意的是,两个while的条件之间的关系。代码如下:

 /**
* 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 *nList=new ListNode(-);
nList->next=head;
ListNode *pre=nList;
ListNode *fast=head;
ListNode *slow=head;
int num=; while(num++<n)
{
fast=fast->next;
}
while(fast->next)
{
pre=pre->next;
slow=slow->next;
fast=fast->next;
}
pre->next=slow->next; return nList->next;
}
};

[Leetcode] remove nth node from the end of list 删除链表倒数第n各节点的更多相关文章

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

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

  3. 【LeetCode每天一题】Remove Nth Node From End of List(移除链表倒数第N个节点)

    Given a linked list, remove the n-th node from the end of list and return its head. Example:        ...

  4. LeetCode第[19]题(Java):Remove Nth Node From End of List(删除链表的倒数第N个节点)

    题目:删除链表的倒数第N个节点 难度:Medium 题目内容: Given a linked list, remove the n-th node from the end of list and r ...

  5. 25.Remove Nth Node From End of List(删除链表的倒数第n个节点)

    Level:   Medium 题目描述: Given a linked list, remove the n-th node from the end of list and return its ...

  6. 19. Remove Nth Node From End of List[M]删除链表的倒数第N个节点

    题目 Given a linked list, remove the n-th node from the end of list and return its head. *Example: Giv ...

  7. 力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现

    题目描述: 中文: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二 ...

  8. LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)

    题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->n ...

  9. 19. Remove Nth Node From End of List C++删除链表的倒数第N个节点

    https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 使用双指针法,可以仅遍历一次完成节点的定位 /** * Definiti ...

随机推荐

  1. php 移动或重命名文件(图片)到另一目录下的方法有多种,这里只列出三种:

    php 移动或重命名文件(图片)到另一目录下的方法有多种,这里只列出三种:       方法一:使用copy函数   格式:copy(source,destination)   将文件从 source ...

  2. 学习photoshop心得

    简要的学习了ps的三大功能p图,抠图,作图, p图主要是学了换脸这一效果,用到套索工具,把范冰冰的脸接到郭德纲身上, 首先使用套索工具把脸圈起来 然后移动到 另一个人脸上 再然后混合图层,自动混合 差 ...

  3. Table被web编程弃用的原因

    Table要比其它html标记占更多的字节. (延迟下载时间,占用服务器更多的流量资源.)Tablle会阻挡浏览器渲染引擎的渲染顺序. (会延迟页面的生成速度,让用户等待更久的时间.)Table里显示 ...

  4. 在WPF中创建可换肤的用户界面

    原文:在WPF中创建可换肤的用户界面 在WPF中创建可换肤的用户界面.                                                                  ...

  5. 用intellij Idea加载eclipse的maven项目全流程

    eclipse的maven项目目录 全流程 加载项目 打开intellij Idea file -> new -> module from existing Sources  选择.pom ...

  6. React16版本的新特性

    React16版本更新的新特性 2018年05月03日 21:27:56 阅读数:188 1.render方法的返回值类型:New render return types 之前的方式: class A ...

  7. 【数据库】 SQL 通配符

    [数据库] SQL 通配符 1. % : 替代一个或多个字符 2. _ : 仅替代一个字符 3. [] : 字符列中的任何单一字符 4. [^charlist] 或者 [!charlist]  : 不 ...

  8. IOException: win32 io returned 267. Path:

    unity3d在导出android项目时出现了这个错误,找了一圈也没找到原因,最后把项目名中空格去掉后OK了,坑啊!!!!

  9. windows中vim以及cmder的使用

    虽然有gvim,但是我依然更喜欢控制台(可理解为博主的偏执已经发展到某个阶段). windows自带的控制台很糟糕,尤其是我正在用的win7竟然没有全屏功能.任何一个占领屏幕的图标显然是不可忍受的. ...

  10. SVN脱离锁定的几种方法

    SVN经常出现被锁定而无法提交的问题,选择解锁又提示没有文件被锁定,很是头疼.这里整理了一下SVN 被锁定的几种解决方法: 1.出现这个问题后使用“清理”即"Clean up"功能 ...