Remove Nth Node From End of List [LeetCode]
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.
Summary: Be careful about corner cases, like n = 1, or size of linked list equals to n.
ListNode *removeNthFromEnd(ListNode *head, int n) {
vector<ListNode *> cache; // size should be n + 1 or n (in this case, size of linked list is n)
ListNode * current = head;
while(current != NULL){
cache.push_back(current);
if(cache.size() > n + )
cache.erase(cache.begin());
current = current -> next;
} if(cache.size() == n + ){
cache[]->next = cache[1]->next;
return head;
}else {
return cache[0]->next;
}
}
Remove Nth Node From End of List [LeetCode]的更多相关文章
- Remove Nth Node From End of List leetcode java
题目: 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 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 【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解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
- Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- 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 ...
- 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- Merge Two Sorted Lists & Remove Nth Node From End of List
1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...
随机推荐
- 【转载】为什么CPU有多层缓存
原文:为什么CPU有多层缓存 http://mp.weixin.qq.com/s?__biz=MzI1NDM2Nzg5Mw==&mid=2247483712&idx=1&sn= ...
- ubuntu下chromium 安装flash player
原文地址 :http://blog.sina.com.cn/s/blog_858820890102v63w.html 不记得从什么时候起,Chromium 不再支持 Netscape plugin A ...
- thinkphp的自动完成功能说明
手册里有一句话很关键: 自动完成是ThinkPHP提供用来完成数据自动处理和过滤的方法,使用create方法创建数据对象的时候会自动完成数据处理. 这句话说明自动完成发生的时间是create()组建数 ...
- PHP clone
PHP clone 定义一个电视类 class Tv{public $width=100;public function setWidth($v){$this->width = $v; ...
- hdu 1568 Fibonacci 快速幂
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- FLASH CC 2015 CANVAS 实际应用过程中遇到的【粉色】问题(不定期更新)
1,导入音乐导致发布卡死 一开始以为是不支持,FQ搜索了一些帖子,也有说不能再时间轴加音乐,需要用代码加入,想想不太可能啊,如果真的不能为什么IDE不禁用呢? 而实际问题是: 我使用的其中一条音效有问 ...
- luabind 导出string问题
luabind导出字符串 不能导出char* 会有问题 应该是字符串连接的时候出错了 static _TCHAR* pRetChar = new _TCHAR[10]; memcpy(pRetChar ...
- mysql 理解 int(11)
1.这里的int(11) 与int的大小和存储字节,没有一毛钱关系,int的存储字节是4个字节,最大值为 65536*65536 = 40多亿,对于有符号的int,是20多亿.2.那么这里的(11) ...
- Android ViewFlipper控件实例
使用ViewFlipper实现两张图片切换效果,废话不多说,直接上代码. java源码: package com.example.viewflipper; import android.os.Bund ...
- apt系统中sources.list文件的解析
/etc/apt/sources.list 一般源信息都存在这个文件中.但众多软件源都放在一个文件中实在有点乱,于是新版ubuntu也有了分类的方法: 文件夹 /etc/apt/sources.li ...