【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,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
题解:
快慢指针。
/**
* 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)
return head;
ListNode dummy(-);
dummy.next = head;
ListNode* fast = &dummy, *slow = &dummy; while (n-- >= && fast) fast = fast->next; while(fast) {
fast = fast->next;
slow = slow->next;
} ListNode* tmp = slow->next;
slow->next = slow->next->next;
delete tmp; return dummy.next;
}
};
【LeetCode】019. Remove Nth Node From End of List的更多相关文章
- 【LeetCode】19. Remove Nth Node From End of List (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- 【LeetCode】19. Remove Nth Node From End of List
题目: 思路:如果链表为空或者n小于1,直接返回即可,否则,让链表从头走到尾,每移动一步,让n减1. 1.链表1->2->3,n=4,不存在倒数第四个节点,返回整个链表 扫过的节点依次:1 ...
- 【一天一道LeetCode】#19. Remove Nth Node From End of List
一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- 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】1019. Next Greater Node In Linked List
题目如下: We are given a linked list with head as the first node. Let's number the nodes in the list: n ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
随机推荐
- Jmeter 05 JMeter元件详解
1. JMeter 逻辑控制器 Switch条件控制器.While条件控制器.交替控制器.仅一次控制器.随机控制器.随机顺序控制器.条件控制器(如果(if)).循环控制器.录制控制器.ForEach控 ...
- 爬虫入门【5】PyQuery简介
PyQuery 目前最新的版本是1.3,基于最新版本进行介绍. 主要根据PyQuery的官方文档进行了更新. from pyquery import PyQuery as pq from lxml i ...
- window.scrollTo(0,document.body.scrollHeight)
滑轮到页面底部 左下角window.scrollTo(0,document.body.scrollHeight) 右下角window.scrollTo(document.body.scrollWidt ...
- python+NLTK 自然语言学习处理四:获取文本语料和词汇资源
在前面我们通过from nltk.book import *的方式获取了一些预定义的文本.本章将讨论各种文本语料库 1 古腾堡语料库 古腾堡是一个大型的电子图书在线网站,网址是http://www.g ...
- GitHub提示 Error: Key already in use解决办法
GitHub提示 Error: Key already in use解决办法GitHub提示 Error: Key already in use解决办法2014年09月05日 ⁄ 综合 ⁄ 共 290 ...
- Kattis - horrorfilmnight 【贪心】
题意 有两个人想去一起看电影,然后分别给出两个人 分别喜欢看的电影都在哪些天 然后 同一个人 不能连续看两天他不喜欢的电影 求他们最多可以看多少次电影 思路 先将两人喜欢看的电影进行排序, ① 选择两 ...
- Hive与Hbase关系整合
近期工作用到了Hive与Hbase的关系整合,虽然从网上参考了很多的资料,但是大多数讲的都不是很细,于是决定将这块知识点好好总结一下供大家分享,共同掌握! 本篇文章在具体介绍Hive与Hbase整合之 ...
- hadoop1.2.1 datanode 由于权限无法启动 expected: rwxr-xr-x
/************************************************************ STARTUP_MSG: Starting DataNode STARTUP ...
- c++学习笔记(网上资料)
C++笔记 2007-3-22 1. 程序 —— 可执行文件,人发送给计算机的一组指令. 硬件指令是二进制, ...
- 每天一个Linux命令(17)whereis命令
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. (1)用法: 用法: w ...