https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/

remove倒数第n个节点

一般list remove node的题目,都要先设置一个 dummy 节点, dummy->next = head,最后返回 dummy->next。

因为有可能要删除的就是head节点,这样不用再额外判断了。

/**
* 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) {
if(head==NULL || n<=)
return head; ListNode *dummy = new ListNode(-);
dummy->next = head;
ListNode *fast = dummy, *slow = dummy; while(n--)
{
if(fast == NULL)
return dummy->next;
fast = fast->next;
}
while(fast->next)
{
fast = fast->next;
slow = slow->next;
}
if(slow->next)
slow->next = slow->next->next; return dummy->next;
}
};

LeetCode OJ-- Remove Nth Node From End of 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 ...

  10. LeetCode(46)-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, ...

随机推荐

  1. 对数据仓库Hive的一些认识

    首先我们得明白什么是数据仓库?   数据仓库,英文名称为Data warehouse,可简写为DW或DWH.数据仓库的目的是构建面向分析的集成化数据环境,为企业提供决策支持(Decision Supp ...

  2. Java课堂作业

  3. 迭代器Iterator与语法糖for-each

    一.为什么需要迭代器 设计模式迭代器 迭代器作用于集合,是用来遍历集合元素的对象.迭代器迭代器不是Java独有的,大部分高级语言都提供了迭代器来遍历集合.实际上,迭代器是一种设计模式: 迭代器模式提供 ...

  4. VMWare workstation Pro 14 For Linux key

    VMWare workstation Pro 14 For Linux key: (我使用的Linux 系统是 Ubuntu16.04, 64位 ) 镜像是官方网址下载的,你也可以自己去官方网址下载: ...

  5. 【STM32】IIC的基本原理(实例:普通IO口模拟IIC时序读取24C02)(转载)

     版权声明:本文为博主原创文章,允许转载,但希望标注转载来源. https://blog.csdn.net/qq_38410730/article/details/80312357 IIC的基本介绍 ...

  6. POJ3436------ACM Computer Factory

    题目链接 ACM Computer Factory Description As you know, all the computers used for ACM contests must be i ...

  7. 11、python中的函数(基础)

    一.什么是函数? 在数学中,x2+2x2+3=10这样的叫方程. 而ax2+bx2+c=d这样的才叫函数.数学的函数中,abcd等待输入的未知量叫自变量,它需要我们自己去输入,而x这种待求得未知量叫因 ...

  8. CCPC_1005

    可怕.....的提.....显而易见的规律活活没照出来...不过说起来却是不能严格证明....于是...脑筋急转弯活活猜不出来..... 1*1->1*2->2*2->2*3-> ...

  9. git上传自己的代码

    感谢这个哥们的博客,不过里面有些错误. http://www.cnblogs.com/ruofengzhishang/p/3842587.html 下面是我自己的实践成功的: 这篇文章写得是windo ...

  10. xgboost原理总结和代码展示

    关于xgboost的学习推荐两篇博客,每篇看2遍,我都能看懂,你肯定没问题 两篇方法互通,知识点互补!记录下来,方便以后查看 第一篇:作者:milter链接:https://www.jianshu.c ...