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.

只能走一遍,要求移除倒着数的第n个元素,开始想用递归做,但是一直失败,没办法看了一下别人的答案,如果总数为N那么倒着第n个相当于正着第N-n个,N的计数通过一次遍历计数即相对的可以得到,代码如下:

 /**
* 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 * p, * q, * pPre;
pPre = NULL;
p = q = head;
while(--n > )
q = q->next;
while(q->next){
pPre = p;
p = p->next;
q = q->next; }
if(pPre == NULL){
head = p->next;
delete p;
}else{
pPre->next = p->next;
delete p;
}
return head;
}
};

LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)的更多相关文章

  1. LeetCode 019 Remove Nth Node From End of List

    题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...

  2. leetcode 【 Remove Nth Node From End of List 】 python 实现

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

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

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

  5. 【leetcode】Remove Nth Node From End of List(easy)

    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

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

  7. 【JAVA、C++】LeetCode 019 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 ...

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

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

随机推荐

  1. J2EE之JPA

    POJO(plain old java object普通java类):具有setter/getter方法的Java类就称为POJO POJO转化为实体,手工使用标记, @Entity public c ...

  2. 六顶思维帽的思考,敏捷开发?——By Me

    人类的思维可以分为很多种,其中按照思维的深度和广度的侧重,可以分为纵向思维和横向思维两种: 简单的来说,“六顶思维帽”可以简单的理解为下图所示: 如何使用这种思维方式呢?举个例子:先输入一个待讨论的事 ...

  3. Java基础—枚举

    定义 枚举(enum)类型是Java 5新增的特性,它是一种新的类型,允许用常量来表示特定的数据片断,而且全部都以类型安全的形式来表示. 为什么要用枚举 在java语言中还没有引入枚举类型之前,表示枚 ...

  4. http的请求流程

    # !/usr/bin/env python # coding:utf-8 import socket def handle_request(client): buf = client.recv(10 ...

  5. 微信小程序组件checkbox

    表单组件checkbox:官方文档 Demo Code: JS Page({ data:{ items:[ {name: 'USA', value: '美国'}, {name: 'CHN', valu ...

  6. 自动化测试管理平台ATMS(V1.0.1_7.29)下载

    自动化测试管理平台ATMS(V1.0.1_7.29)下载http://automationqa.com/forum.php?mod=viewthread&tid=2582&fromui ...

  7. 20145217《网络对抗》 MSF基础应用

    20145217<网络对抗> MSF基础应用 MSF基础应用 1.实践任务 任务一:ms08_067渗透攻击 任务二:IE浏览器渗透攻击--MS12063安全漏洞 任务三:adobe渗透攻 ...

  8. Eclipse调试C++(Cocos2dx Android )

    原文链接: http://www.cnblogs.com/zouzf/p/4202537.html 先说windows下的,mac下的在最后 环境:win8.1.java 1.5.Eclipse 4. ...

  9. Gradle 引入本地定制 jar 包,而不使用坐标下载 jar 包的方法

    第 1 步:创建文件夹,拷贝 jar 包 在自己的 Gradle 项目里建立一个名为 “libs” (这个名字可以自己定义,不一定非要叫这个名字)的文件夹,把自己本地的 jar 包拷贝到这个文件夹中. ...

  10. Execute Disable Bit

    “Execute Disable Bit”是Intel在新一代处理器中引入的一项功能,开启该功能后,可以防止病毒.蠕虫.木马等程序利用溢出.无限扩大等手法去破坏系统内存并取得系统的控制权.其工作原理是 ...