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. 如何修改eclipse的默认字符集和修改中文乱码

    转载,以供以后学习.谢谢 有时候 java代码,导入eclipse中会出现 乱码的问题,通过修改字符集就可以解决. 看下面图片演示过程. 发表在 使用教程 | 标签为 eclipse, 乱码 | 留下 ...

  2. template.js文档

    参见GitHub:https://github.com/yanhaijing/template.js/ template.js简介: template.js 一款javascript模板引擎,简单,好 ...

  3. hashCode与equals的作用与区别及应当注意的细节

    最近去面试了几家公司,被问到hashCode的作用,虽然回答出来了,但是自己还是对hashCode和equals的作用一知半解的,所以决定把它们研究一下. 以前写程序一直没有注意hashCode的作用 ...

  4. wifi认证Portal开发系列(二):FreeRadius的安装和测试、关联Mysql

    注:本次安装是基于FreeRadius 3版本进行安装配置的,在配置Mysql的过程中,与2版本有些不同.操作系统是CentOS 7 一.准备工作 工具的安装 #安装rz.sz命令用于文件上传 yum ...

  5. jQuery Validate(三)

    这里,我们再说说radio.checkbox.select的验证方式. 1.用新版的写法进行验证. <!DOCTYPE html> <html> <head> &l ...

  6. 【puppeteer+Node.js安装环境】之WebStorm编辑器运行失败问题:Error: Cannot find module 'puppeteer'并且代码出不来“asnyc”标识以及有红色波浪线解决办法

    现象一: module.js:557     throw err;     ^ Error: Cannot find module 'puppeteer'  at Function.Module._r ...

  7. 自我总结- CGAffineTransform

    在应用中我们经常需要做一些仿射变换 可以用于 平移.旋转.缩放变换路径: View有一个属性transform 可以指定一个 CGAffineTransform 即可完成仿射变换 1.平移变换 // ...

  8. java log4j 日志文件

    开发中经常会用到log日志文件,根据业务需要可能不产生很大日志文件给维护和[排错带来了麻烦.所以我们希望能够每天或每个月产生一个日志文件,这样文件不至于过大. 或者根据日志文件大小来判断,超过规定大小 ...

  9. Python中的staticmethod和classmethod

    谈谈Python中的staticmethod和classmethod 首先值得说明的是staticmethod和classmethod都是python中定义的装饰器,用的时候需要在前面加@ 即@sta ...

  10. 替代或者与 Redis 配合存储十亿级别列表的数据.

    http://ssdb.io/docs/zh_cn/index.html 用户案例 如果你在生产环境中使用 SSDB, 欢迎你给我发邮件(ssdb#udpwork.com), 我很愿意把你加入到下面的 ...