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. Java集合框架汇总

    HashMap是一个最常用的Map,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度,遍历时,取得数据的顺序是完全随机的.HashMap最多只允许一条记录的键为NUL ...

  2. Dialogue between Jack and Rose【jack 和 Rose的对话】

    Dialogue between Jack and Rose Rose : It's getting quiet. 越来越安静了 Jack : It's gonna take a couple of ...

  3. stm32-IIC读写EEPROM—时序说明

    I2C 通讯协议:(Inter-Integrated Circuit)是由Phiilps 公司开发的,由于它引脚少,硬件实现简单,可扩展性强,不需要USART.CAN  等通讯协议的外部收发设备,现在 ...

  4. Keywords Search HDU - 2222 ( ac自动机)模版题

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. Linux基本命令运行

    文件基本操作: 增删查改: 创建文件:touch(创建文件和修改文件或者目录的时间戳),vim.vi(编辑/创建文件),mkdir(创建文件目录) 移动和修改文件名:mv 删除文件:rm –rf(强制 ...

  6. Asp.net页面生命周期详解任我行(3)-服务器处理请求详细过程

    前言 百度了一下才知道,传智的邹老师桃李满天下呀,我也是邹老师的粉丝,最开始学习页面生命周期的时候也是看了邹老师的视频. 本人是参考了以下前辈的作品,本文中也参合了本人心得,绝非有意盗版,旨在传播,最 ...

  7. APP网络测试点

  8. jQuery动态显示和隐藏datagrid中的某一列的方法

    在EasyUI中: 1)展示某列的方法:     $('#jgrid').datagrid('showColumn', 'XXX');  -----其中 XXX 是隐藏列的  field 属性值 2) ...

  9. list dict 性能测试

    from random import randint def load_list_data(total_nums, target_nums): """ 从文件中读取数据, ...

  10. tomcat 启动慢解决(/dev/random)

    JRE默认使用 /dev/random作为随机数来源,当熵池大小不够的时候,random会很慢,造成随机数生成调用阻塞. 解决方案: 改用 /dev/urandom (1) tomcat的启动选项增加 ...