142.Linked List Cycle II---双指针
题目大意:141题目的扩展,给出单链表,判断是否有环,如果有环,找出环的开始的结点,如果没有环,返回null。
法一(借鉴):在已经找出单链表环的基础上再找开始结点,要时刻记住这个环不一定是从尾结点到头结点,有可能是中间的某一段。所以对于这里具体路径的分析,有两个博客可看http://blog.csdn.net/xy010902100449/article/details/48995255,http://blog.csdn.net/willduan1/article/details/50938210,都是从数学层面去证明方法的可行性,也就是为什么可以在找到相遇点后,再从头开始遍历,第二次相遇点就是开始结点的问题。代码如下(耗时1ms):
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if(fast == slow) {
fast = head;
while(fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
return null;
}
法二:利用了141的法二,直接用set存入,如果有相同的则说明肯定是环的开始结点,直接返回即可。此方法的时间复杂度和空间复杂度都挺大的。代码如下(耗时16ms):
Set<ListNode> list = new HashSet<>();
while(head != null) {
if(list.contains(head)) {
return head;
}
else {
list.add(head);
head = head.next;
}
}
return null;
142.Linked List Cycle II---双指针的更多相关文章
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- Java for LeetCode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【LeetCode】142. Linked List Cycle II (2 solutions)
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- (链表 双指针) leetcode 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- [LeetCode] 142. Linked List Cycle II 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【LeetCode】142. Linked List Cycle II
Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...
随机推荐
- 洛谷 P2146 [NOI2015]软件包管理器
真没有想到,这竟然会是一道NOI的原题,听RQY说,这套题是北大出的,北大脑抽认为树剖很难... 只恨没有早学几年OI,只A这一道题也可以出去吹自己一A了NOI原题啊 好了,梦该醒了,我们来看题 以后 ...
- Qt5 UI信号、槽自动连接的控件重名
Qt5 UI信号.槽自动连接的控件重名 来源 http://blog.csdn.net/goldenhawking/article/details/51865909 对Qt5稍有熟悉的童鞋都知道信号. ...
- 【BZOJ4596】黑暗前的幻想乡(矩阵树定理,容斥)
[BZOJ4596]黑暗前的幻想乡(矩阵树定理,容斥) 题面 BZOJ 有\(n\)个点,要求连出一棵生成树, 指定了一些边可以染成某种颜色,一共\(n-1\)种颜色, 求所有颜色都出现过的生成树方案 ...
- BIOS和CMOS的区别
原文链接:https://www.cnblogs.com/boltkiller/articles/5732424.html 在日常操作和维护计算机的过程中,常常可以听到有关BIOS设置和CMOS设置的 ...
- vim 单文件中查找方法
1.vim 单文件中查找方法 正常模式下使用 / 或 ? 命令执行向后搜索或向前搜索 /love 从光标位置向前搜索关键词 love ?love 从光标位置向后搜索关键词 love 正常模式下 ...
- scrapy 采集网页出现丢失url的问题
url_list = ["http://www.icoat.cc/news/list_18_3.html", "http://www.icoat.cc/news/list ...
- HTTP、TCP、 UDP、 IP 、 Socket的关系
TCP和UDP协议是传输协议,IP是网络层协议.传输协议和网络层协议主要解决数据如何在网络中传输,或者说TCP/IP 定义了电子设备如何连入因特网,以及数据如何在它们之间传输的标准.TCP负责发现传输 ...
- [DeeplearningAI笔记]卷积神经网络2.3-2.4深度残差网络
4.2深度卷积网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 [残差网络]--He K, Zhang X, Ren S, et al. Deep Residual Learni ...
- OpenCV---色彩空间(二)HSV追踪颜色对象和通道分离与合并
一:HSV追踪有颜色对象 def inRange(src, lowerb, upperb, dst=None) #lowerb是上面每个颜色分段的最小值,upperb是上面每个颜色分段的最大值,都是列 ...
- vim 常用快捷键(整理版)
最常用: x 删除后面的字符 X 删除前一个字符 删除3个字符就是3x dd:删除一行 D 删除到行尾 J:删除换行符,使下一行并上来. nJ:连接后面的n行 u:撤销上一次操作 ...