LeetCode OJ-- Remove Nth Node From End of List
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的更多相关文章
- 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 ...
- 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, ...
- [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 ...
- 【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 ...
- 【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 ...
- [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, ...
- 【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 ...
- 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 ...
- 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 ...
- 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, ...
随机推荐
- Scrapy-redis分布式爬虫爬取豆瓣电影详情页
平时爬虫一般都使用Scrapy框架,通常都是在一台机器上跑,爬取速度也不能达到预期效果,数据量小,而且很容易就会被封禁IP或者账号,这时候可以使用代理IP或者登录方式爬,然而代理IP很多时候都很鸡肋, ...
- PAT basic 1087
1087 有多少不同的值 (20 分) 当自然数 n 依次取 1.2.3.…….N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然 ...
- Linux下安装nginx,以及启动和停止
1.安装 安装nginx之前,首先确保系统已经安装了依赖:g++.gcc.openssl-devel.pcre-devel和zlib-devel软件 yum install gcc-c++ yum - ...
- ActiveMQ初步学习
本文主要参考张丰哲大神的简书文章,链接 https://www.jianshu.com/p/ecdc6eab554c JMS,即Java Message Service,通过面向消息中间件(MOM:M ...
- cf982d Shark
ref #include <algorithm> #include <iostream> #include <cstdio> #include <map> ...
- flex布局之flex-grow和flex-shrink如何计算
此文已由作者张含会授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 关于盒模型布局 如何实现两栏布局? (表格) 流式, 浮动, 定位 如何选择? 流式 > 浮动 > ...
- scripts
Shell Script:================================================================ 示例1:[root@server0 ~]# ...
- Python 操作 SQLite 数据库
写在之前 SQLite 是一个小型的关系型数据库,它最大的特点在于不需要单独的服务.零配置.我们在之前讲过的两个数据库,不管是 MySQL 还是 MongoDB,都需要我们安装.安装之后,然后运行起来 ...
- startActivityForResult用法
使用场景:A界面(activity) 可跳转到一个(假设为 B)或多个子Activity,要求B处理完工作之后返回A 界面,或许同时返回一些数据交给A继续处理.如 由登陆界面A跳转到注册界面B,注册成 ...
- Leetcode 599.两个列表的最小索引总和
两个列表的最小索引总和 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答 ...