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. ElasticSearch : 基础

    #新建索引以及类型: PUT http://10.18.43.3:9200/test { "settings": { "number_of_shards": 3 ...

  2. 使用C6748和C5509A对nRF24L01驱动进行数据传输

    1. 写在前面 今天下午做了一个C5509A和C6748两个DSP的数据传输,经由RF24L01设备传输,都是模拟SPI协议,对于两个DSP来说,无非是配GPIO引脚,写好时序和延时.C5509A的G ...

  3. Python自动化运维——IP地址处理模块

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 模块:IPy 功能:辅助我们高效的完成IP的规划工作 安装: wget https://pypi.python.o ...

  4. (数据科学学习手札16)K-modes聚类法的简介&Python与R的实现

    我们之前经常提起的K-means算法虽然比较经典,但其有不少的局限,为了改变K-means对异常值的敏感情况,我们介绍了K-medoids算法,而为了解决K-means只能处理数值型数据的情况,本篇便 ...

  5. JQuery中的load()、$.get()和$.post()详解 (转)

    load() 1.载入HTML文档 load()方法是jQuery中最为简单和常用的Ajax方法,能载入远程HTML代码并插入DOM中. 它的结构为: load(url [,data][,callba ...

  6. 利尔达NB-IOT模块对接移动onenet平台步骤

    1. 首先登陆浙江移动onenet网站,http://openiot.zj.chinamobile.com/,进入右上角的开发者中心,然后才能看到创建产品 2. 填写产品的信息,其他信息按照个人实际填 ...

  7. ubuntu下安装redis及在php中使用

    一.安装redis sudo apt-get install redis-server 安装完成后,Redis服务器会自动启动,我们可以通过下面的命令行检查一下: # redis-cli > p ...

  8. spring location设置本地路径

    <context:property-placeholder location="file:D:/jdbc.properties"/> 直接在路径前加上 file:

  9. MySQL☞create语句

    几种常用的建表语句: 1.最简单的建表语句: create table 表名( 列名1 数据类型(长度), 列名2 数据类型(长度), ... ) 2.带主键的建表语句: CREATE TABLE 表 ...

  10. spring boot 打包问题

    一.jar包 1.maven build package 2.linux 下执行 java -jar & 命令后台运行,也可加入服务运行 二.war包 1.将pom中的<packagin ...