(链表 双指针) leetcode 19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head.
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.
Follow up:
Could you do this in one pass?
-------------------------------------------------------------------------------------------------------------------------
这个题就是移去从表尾开始的第n 个元素。emmmm,这个最好画图,便于理解。
可以用双指针。
C++代码:
/**
* 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) {
ListNode *s = head;
ListNode *e = head;
if(!head) return NULL;
for(int i = ; i < n; i++)
e = e->next;
if(!e) return head->next;
while(e->next){
s = s->next;
e = e->next;
}
s->next = s->next->next;
return head;
}
};
(链表 双指针) leetcode 19. Remove Nth Node From End of List的更多相关文章
- [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]19. Remove Nth Node From End of List删除链表倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- [LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 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 19. Remove Nth Node From End of List [Difficulty: Medium]
题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...
- [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 [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] 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 ...
随机推荐
- LodopFuncs.js和CLodopFuncs.js区别和联系
所在位置:LodopFuncs.js可以在官网下载中心综合版里下载到.CLodopfuncs.js在C-Lodop服务缓存中,C-Lodop启动的时候才能访问到. 需不需要下载放置到项目里:(客户端本 ...
- Python学习之路—————day04
今日内容: 1. 循环语句 1.1 if判断 1.2 while循环 1.3 for循环 一.if判断 语法一: if 条件 代码块1 代码块2 代码块3 # 例: sex='female' age= ...
- webpack始出来
一直想好好整理一下webpack,现在就整理吧. 总结自己的实际搭建的整理情况,我还是要先对自己说一句,以后给文件夹起名字的时候不要用一些特殊的关键字,比如我在做这个demo的时候,我用的文件夹名称叫 ...
- Tunnel Warfare(线段树取连续区间)
emmmmmmmm我菜爆了 思路来自:https://blog.csdn.net/chudongfang2015/article/details/52133243 线段树最难的应该就是要维护什么东西 ...
- 在idea中设置记住git的用户名和密码
在idea中设置记住git的用户名和密码 1.在项目根目录下执行以下git命令: git config --global credential.helper store 2.执行上述命令后,在idea ...
- xshell使用rz/sz完成文件上传下载
yum -y install lrzsz 安装lrzsz 使用rz完成文件上传 使用sz完成文件下载
- pysphere VMware控制模块的一些函数的说明
对于虚拟机的操作获得虚拟机对象 当你正常连接了服务器后,你就可以使用以下两种方式来得到虚拟机对象. get_vm_by_path get_vm_by_name 虚拟机路径可以从虚拟机右键信息中的”Ed ...
- Codeforces Round #426 Div. 1
A:考虑每个质因子,显然要求次数之和是3的倍数,且次数之差的两倍不小于较小的次数.对于第一个要求,乘起来看开三次方是否是整数即可.第二个取gcd,两个数分别除掉gcd,之后看两个数的剩余部分是否都能被 ...
- Android InputType
转载: http://blog.csdn.net/wei_zhi/article/details/50094503 在Android开发过程中,我们经常使用到EditText控件,并且会根据各种需求设 ...
- 洛谷P1462通往奥格瑞玛的道路题解
[题目]: https://www.luogu.org/problemnew/show/P1462 题意 题目是给定了一张双向边,有边权的图,然后让我们求出一个最小值,满足一条路径上的最大的费用小于这 ...