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.

简单的指针操作。

/**
* 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 *pn=head;
int sum=;
while(pn->next){
pn=pn->next;
sum++;
}
int tn=sum-n+;
if(tn==){
return head->next;
}
pn=head;
int i=;
while(i!=tn-){
pn=pn->next;
i++;
}
pn->next = pn->next->next;
return head; }
};

leetcode 19. Remove Nth Node From End of List(链表)的更多相关文章

  1. Leetcode 19 Remove Nth Node From End of List 链表

    删除从后往前数的第n个节点 我的做法是将两个指针first,second 先将first指向第n-1个,然后让first和second一起指向他们的next,直到first->next-> ...

  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 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, ...

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

  5. 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 ...

  6. (链表 双指针) leetcode 19. Remove Nth Node From End of List

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

  7. [leetcode]19. 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: Given l ...

  8. 蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]

    题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...

  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删除链表的倒数第N个节点

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

随机推荐

  1. nodejs读取配置文件

    INI.js(模块) var eol = process.platform === "win32" ? "\r\n" : "\n" func ...

  2. 17:不重复整数提取NoRepeatNum

    题目描述 输入一个int型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数. 输入描述:输入一个int型整数 输出描述:按照从右向左的阅读顺序,返回一个不含重复数字的新的整数 输入例子: ...

  3. java 内存与内存溢出

    学习自:http://www.codeceo.com/article/jvm-memory-overflow.html 讲的很清楚

  4. deeplearning.net 0.1 document - Multilayer Perceptron

    Multilayer Perceptron 以下我们使用Theano来介绍一下单隐藏层的多层感知机(MLP).MLP能够看成一个logistic回归分类器,它使用一个已经学习的非线性转换器处理输入.这 ...

  5. Linux kernel 2.6下的modules编译与KBuild

    转载:http://blog.sina.com.cn/s/blog_602f87700100dq1u.html Sam之前在Linux kernel 2.4下写过一些driver.但自从转到kerne ...

  6. 【NOI2015】【程序自己主动分析】【并查集+离散化】

    Description 在实现程序自己主动分析的过程中,经常须要判定一些约束条件能否被同一时候满足. 考虑一个约束满足问题的简化版本号:如果x1,x2,x3,-代表程序中出现的变量.给定n个形如xi= ...

  7. 华为基于策略划分VLAN的配置方法及示例

     学过思科交换机的朋友,可能对基于策略划分VLAN的配置方法印象非常深,感觉确实比较复杂,先要配置VMPS以及VMPS数据库,但在华为交换机中,这种现象得到了彻底改变,因为它有了一种特殊的端口类型—— ...

  8. ASP.NET动态网站制作(3)--css(2)

    前言:css分为四次课讲完,第一节课内容见ASP.NET动态网站制作(2)--css(1),接下来的内容会涉及到定位.浮动.盒子模型(第二次课).css的具体应用(第三次课).css3(第四次课).今 ...

  9. iOS - 集成SDK问题

    1.大部分社交平台接口不支持https协议. 问题描述:在iOS9下,系统默认会拦截对http协议接口的访问,因此无法获取http协议接口的数据.对ShareSDK来说,具体表现可能是,无法授权.分享 ...

  10. Eclipse + JDK + tomcat开发环境配置

       第一步:下载jdk和tomcat:JDK下载  Tomcat下载     最新的jdk为1.6.10,tomcat为6.0,建议jdk1.4以上,tomcat4.0以上    第二步:安装和配置 ...