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. node.js---sails项目开发(3)

    1.为新建的sails数据库新建一个用户,首先连接数据库 mongo localhost:27017 (1)显示所有数据库   (2)切换数据库 show dbs use sails 新建一个用户 账 ...

  2. ansible安装及使用

    一.ansible介绍 1.ansible简介 官方的title是“Ansible is Simple IT Automation”——简单的自动化IT工具. Ansible跟其他IT自动化技术的区别 ...

  3. for迭代序列的三种方式

    while循环是条件性的,for循环是迭代性的. for循环会访问所有迭代对象中的所有元素,并在所有条目都结束后结束循环. for循环迭代序列有三种基本的方式,分别是通过序列项迭代.通过索引迭代.通过 ...

  4. Unity,基于layer的碰撞配置

    可以通过给对象指定layer,实现相同.不同layer之间碰撞的自由配置,比如我们想让怪物之间不碰撞,英雄和怪物之间碰撞,我们就可以这样指定,去掉enemies之间的勾选. 打开方式-edit-> ...

  5. NAT配置与管理

    为解决IPv4地址日益枯竭,出现NAT(Network Address Translation,网络地址转换)技术.NAT可以将来自一个网络的IP数据报报头中的IP地址(可以是源IP地址或目的IP地址 ...

  6. 无线安全之破解WPA/WPA2 加密WiFi

    准备 可以使用无线网络的Kali Linux 由于古老的WPE加密的WiFi已经几乎没有了,所以这里我就不去细说如何破解WPE加密的WiFi了.今天就来聊聊 如何来使用Kali Linux来破解Wpa ...

  7. Mysql 忘记密码处理配置

    Mysql忘记密码处理 1.设置mysql密码 mysqladmin -uroot password ‘密码’ 2.主配置文件下取消密码授权 vim /etc/my.cnf 注:加入skip-gran ...

  8. 基于HTML5和SVG的手机菜单动画

    在线演示 本地下载

  9. grep 使用场景

    (1)结合find命令和管道   你的一个音乐文件夹里有多种格式的文件,而你只想找到艺术家jay的mp3文件,并且不含有任何的混合音轨 find . -name "*mp3" | ...

  10. java中Vector类的常用方法

    Vector类是实现List接口,所以继承的方法就不在这里讲了 https://www.cnblogs.com/xiaostudy/p/9503199.html public void add(int ...