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. requests.exceptions.SSLError……Max retries exceeded with url错误求助!!!

    import requests head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Appl ...

  2. DFS:BZOJ1085-骑士精神

    题目: 1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1461  Solved: 796 [Submit][ ...

  3. [原]sencha touch之panel和tabpanel

    最近在弄senchatouch的项目,所以边学习边开发,边记录,直接记录下test code如下: Panel: Ext.application({ name:'itKingApp', launch: ...

  4. Linuxshell编程

    重点回顾: 1.由于核心的内存中是受保护的区块,因此我们必须要透过“shell”将我们输入的指令与Kernel沟通,好让Kernel可以控制硬件来正确无误的工作 2.学习shell的原因主要有:文字接 ...

  5. IOS开发学习笔记021-练习2

    只是简单练习一下,主要是学习里面的思想,处理问题的方法. 不过还有一个问题没想到解决方法. 那就是动态生成的按钮如何绑定按钮事件,请哪位大神指点一下啊.(知道怎么办了,原来是方法addTarget) ...

  6. Halcon18 windows 下载

    Halcon18 windows 下载地址:http://www.211xun.com/download_page_13.html HALCON 18 是一套机器视觉图像处理库,由一千多个算子以及底层 ...

  7. RESTful-rest_framework版本控制、分页器-第六篇

     版本控制: 源码位置分析第一步: 源码位置分析第二步:在APIView-despath方法-initial-determine_version #执行determine_version,返回两个值, ...

  8. [oldboy-django][深入 rest framework] restframewok 教程: 分页功能

    http://www.django-rest-framework.org/api-guide/pagination/ https://stackoverflow.com/questions/31785 ...

  9. hadoop配置文件: hdfs-site.xml, mapred-site.xml

    dfs.name.dir Determines where on the local filesystem the DFS name node should store the name table( ...

  10. 【bzoj1095】[ZJOI2007]Hide 捉迷藏 动态点分治+堆

    题目描述 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏.他们的家很大且构造很奇特,由N个屋子和N-1条双向走廊组成,这 ...