LeetCode Linked List Cycle II
来源:互联网 发布:白狐小说网站源码 编辑:程序博客网 时间:2024/06/06 07:01
LeetCode解题之Linked List Cycle II
原题
如果给定的单向链表中存在环,则返回环起始的位置,否则返回为空。最好不要申请额外的空间。
注意点:
- 不要修改链表
例子:
输入:
1->2->3 | | 5<-4
输出: 2
解题思路
在[Linked List Cycle](141 Linked List Cycle.md)中,我们通过双指针方法来判断链表中是否存在环。在此基础上,我们来找出环的起始节点。如下图所示,假设链表的起始节点为A,环的起始节点为B,快慢指针在C处相遇。因为快指针的速度是慢指针的两倍,所以在相同时间内,它走过的路程是慢指针的两倍,而快指针走过的路程是(x+y+z+y),而慢指针走过的路程是(x+y),根据关系我们可以得到x+y+z+y = 2(x+y)
,也就是说x=z
。此时快慢指针在C处,头指针在A处,而它们到B的距离相等,那么只要有两个指针分别从点A和点C出以相同的速度前进就会在点B处相遇,也就是找到了环的起始节点。
注:上面的情况是理想情况,实际上在点C相遇时,快指针可能已经绕着环走了好几圈了,如AB很长,而环很小的情况。假设走了n圈,此时等式为
x+y+n(z+y) = 2(x+y)
,即x=(n-1)(y+z)+z
,而从点C绕n-1圈后再走z的距离还是会跟从点A出发的指针在点B相遇。
AC源码
# Definition for singly-linked list.class ListNode(object): def __init__(self, x): self.val = x self.next = Noneclass Solution(object): def detectCycle(self, head): """ :type head: ListNode :rtype: ListNode """ slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: node = head while node != slow: node = node.next slow = slow.next return node return Noneif __name__ == "__main__": None
欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源码。
0 0
- 【LeetCode】 Linked List Cycle II
- Leetcode: Linked List Cycle II
- <Leetcode>Linked List Cycle II
- [LeetCode] Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode:Linked List Cycle II
- Leetcode Linked List Cycle II
- [LeetCode]Linked List Cycle II
- leetcode Linked List Cycle II
- LeetCode | Linked List Cycle II
- Leetcode: Linked List Cycle II
- [LeetCode] - Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode - Linked List Cycle II
- leetcode Linked List Cycle II
- Linked List Cycle II -- LeetCode
- LeetCode Linked List Cycle II
- [LeetCode] Linked List Cycle II
- 使用Handler下载数据
- jquery.validate 校验 remote返回值
- TCP/IP入门(3) --传输层
- DH算法证明及相应的理论知识
- C# winform windows服务安装与卸载
- LeetCode Linked List Cycle II
- TCP/IP入门(4) --应用层
- 数据分析面试必备算法[1] - Regression
- 快速排序8
- leetcode 165. Compare Version Numbers-版本比较
- hdu2546饭卡
- C++与类相关的关键词和修饰符说明
- git创建:error: could not lock config file
- UI基础_UIButton