19.Remove Nth Node From End of List---双指针
题目大意:删除单链表中倒数第n个节点。例子如下:

法一:双指针,fast指针先走n步,然后slow指针与fast一起走,记录slow前一个节点,当fast走到链表结尾,slow所指向的指针就是所找的节点。代码如下(耗时8ms):
public ListNode removeNthFromEnd(ListNode head, int n) {
int cnt = 0;
ListNode fast = head, slow = head, preSlow = head;
while(cnt < n) {
fast = fast.next;
cnt++;
}
while(fast != null) {
fast = fast.next;
preSlow = slow;
slow = slow.next;
}
//删除的是第一个节点
if(preSlow == slow) {
head = preSlow.next;
}
//删除的不是第一个节点
preSlow.next = slow.next;
return head;
}
19.Remove Nth Node From End of List---双指针的更多相关文章
- 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 ...
随机推荐
- BZOJ4976 宝石镶嵌(动态规划)
显然被留下的宝石应该贡献至少一位,否则就可以扔掉.所以如果n-k>=logw,直接输出所有数的or.现在n变得和k同阶了.于是设f[i][j]为前i个数or为j时至少选几个数,转移显然.当然可以 ...
- BZOJ3620 似乎在梦中见过的样子(kmp)
不是很懂为什么数据范围要开的这么诡异,想到正解都不敢写.用类似NOI2014动物园的方法,对每个后缀求出类似next的数组即可. #include<iostream> #include&l ...
- hadoop(五)HDFS原理剖析
一.HDFS的工作机制 工作机制的学习主要是为加深对分布式系统的理解,以及增强遇到各种问题时的分析解决能 力,形成一定的集群运维能力PS:很多不是真正理解 hadoop 工作原理的人会常常觉得 HDF ...
- 流媒体协议之RTSP详解20170922
一.RTSP协议介绍 1.什么是rtsp? RTSP协议以客户服务器方式工作,,如:暂停/继续.后退.前进等.它是一个多媒体播放控制协议,用来使用户在播放从因特网下载的实时数据时能够进行控制, 因此 ...
- python学习(十六)写爬虫爬取糗事百科段子
原文链接:爬取糗事百科段子 利用前面学到的文件.正则表达式.urllib的知识,综合运用,爬取糗事百科的段子先用urllib库获取糗事百科热帖第一页的数据.并打开文件进行保存,正好可以熟悉一下之前学过 ...
- 「Linux」VMware安装centos7(二)
一.检查是否联网 命令:ping www.baidu.com 二.查询系统中的地址信息 命令:ip addr 三.查看当前网络连接 命令:nmcli connection show 四.设置网络连接 ...
- 题解 P4092 【[HEOI2016/TJOI2016]树】
参考了皎月半洒花的博客 看到树想到树剖,由于要取距自己到根离自己最近的标记点,刚开始想到线段树里存节点深度,查询时返回最大值.但是这样的话只能得到节点深度,无法得知节点编号,就想倍增乱搞一下,求出标记 ...
- linux的进程1:rootfs与linuxrc
在内核启动的最后阶段启动了三个进程 进程0:进程0其实就是刚才讲过的idle进程,叫空闲进程,也就是死循环.进程1:kernel_init函数就是进程1,这个进程被称为init进程.进程2:kthre ...
- 前端PHP入门-022-重点日期函数之获取本地化时间戳函数.md
在实际的工作中我们还需要经常用到指定某个时间生成 例如:需要找到昨天到今天此时此刻的注册用户. 我们需要做两件事情: 得到当前的时间unix时间戳.用time()函数就可以直接搞定 那么昨天指定时 ...
- 状压dp Gym - 100676G
http://codeforces.com/gym/100676 题目大意: 给你n个科目,m个关系,例如A->B,表示要学习B科目,一定要把A科目学习掉.同理,如果还有C->B,那么,B ...