19.Remove Nth Node From End of List(List; Two-Pointers)
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.
思路:双指针,相差n
/**
* 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* pFirst = head, *pSecond = head;
for(int i = ; i < n; i++){
pFirst = pFirst->next;
}
if(pFirst == NULL){
return head->next;
}
while(pFirst->next){
pFirst = pFirst->next;
pSecond = pSecond->next;
}
pSecond->next = pSecond->next->next;
return head;
}
};
19.Remove Nth Node From End of List(List; Two-Pointers)的更多相关文章
- 61. Rotate List(M);19. Remove Nth Node From End of List(M)
61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 【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个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 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
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
一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...
随机推荐
- Thunder7.2.13.3884 JayXon
更新日志: 更新迅雷7. 更新VipService 2.7 更新SQLite 更新zlib 不再精简msvcr71.dll.msvcp71.dll 不再精简64位BHO 破解了本地离线重命名30个字符 ...
- linux网络监控_网速测试
Linux下查看网络即时网速 1.sar命令(一般般) sar -n DEV 1 100 1代表一秒统计并显示一次 100代表统计一百次 sar在sysstat包 2.使用ntop图形工具(没详细用过 ...
- js字符串的裁剪
一.JavaScript字符串的处理方法 1.split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子: str=”jpg|bmp|gif|ico|png”; arr=str. ...
- mongodb数据处理工具
最近大家需要对mongodb和postgres数据库操作比较频繁,给大家推荐一个数据处理工具Kettle,希望对你能有所帮助 ① 将mongodb数据库中的表进行处理然后导出来生成csv,txt, ...
- 无线路由器的加密模式WEP,WPA-PSK(TKIP),WPA2-PSK(AES) WPA-PSK(TKIP)+WPA2-PSK(AES)。
目前无线路由器里带有的加密模式主要有:WEP,WPA-PSK(TKIP),WPA2-PSK(AES)和WPA-PSK(TKIP)+WPA2-PSK(AES). WEP(有线等效加密)WEP是Wired ...
- 深入理解Java虚拟机,gc输出参数
https://blog.csdn.net/qq_21383435/article/details/80702205
- centos alias命令详解
Alias命令 功能描述:我们在进行系统的管理工作一定会有一些我们经常固定使用,但又很长的命令.那我们可以给这些这一长串的命令起一个别名.之后还需要这一长串命令时就可以直接以别名来替代了.系统中已经有 ...
- [原创]JEECMS 自定义标签调用广告版位下的所有广告(利用广告管理管理首页幻灯片)
JEECMS自带的只有[@cms_advertising]标签,并且官方没有给文档,用法: [@cms_advertising id='3'] <img src=&quo ...
- vmware克隆linux网络配置
一.配置Linux网络 在安装Linux的时候,一定要保证你的物理网络的IP是手动设置的,要不然会在Linux设置IP连通网络的时候会报network is unreachable 并且怎么也找不到问 ...
- Python 中函数和方法
函数与方法 class Foo(object): def __init__(self): self.name = 'lcg' def func(self): print(self.name) obj ...