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. Python学习笔记(三):文件和集合操作

    python string与list互转 因为python的read和write方法的操作对象都是string.而操作二进制的时候会把string转换成list进行解析,解析后重新写入文件的时候,还得 ...

  2. Jack Straws POJ - 1127 (简单几何计算 + 并查集)

    In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table ...

  3. Python登录人人网并抓取新鲜事

    from sgmllib import SGMLParser import sys,urllib2,urllib,cookielib class spider(SGMLParser):     def ...

  4. Android开发——JVM、Dalvik以及ART的区别

    )预编译也可以明显改善电池续航,因为应用程序每次运行时不用重复编译了,从而减少了 CPU 的使用频率,降低了能耗.

  5. OpenCV学习笔记(十) 直方图操作

    直方图计算 直方图可以统计的不仅仅是颜色灰度, 它可以统计任何图像特征 (如 梯度, 方向等等).直方图的一些具体细节: dims: 需要统计的特征的数目, 在上例中, dims = 1 因为我们仅仅 ...

  6. 使用code::blocks编译windows的dll链接库

    因为机子上没有安装Visual Studio,所以找到了一种通过code::blocks编译dll的方式,踩到的坑是code::blocks默认的compiler是32位的,这样编译出的dll也是32 ...

  7. Anaconda的用法

    1. conda list -----显示anacoda安装的模块 2.jupyter notebook----启动网页上的python编程环境 3.anacoda  search -t conda ...

  8. 如何解决Jmeter导出的聚合报告是乱码易位问题

    在使用Jmeter这个工具的时候,有些单词不懂是什么意思,就切换到这个工具自带的中文语言: 当我们测试完毕,导出聚合报告(Summary Report)的时候: 1.有一些第一个Title下面的中文是 ...

  9. UVALive 5987

    求第n个数,该数满足至少由3个不同的素数的乘机组成 #include #include #include #include #include using namespace std; int prim ...

  10. Oracle连接查询小结

    表TESTA,TESTB,TESTC,各有A, B两列 A B 001 10A 002 20A A B 001 10B 003 30B A B 001 10C 004 40C 连接分为两种:内连接与外 ...