[leetcode] Linked List Cycle II
来源:互联网 发布:以色列 巴勒斯坦 知乎 编辑:程序博客网 时间:2024/06/05 10:52
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
Follow up:
Can you solve it without using extra space?
转自:http://www.mysjtu.com/page/M0/S951/951294.html
思路:对于linked list cycle I, 思路很简单,快慢指针相遇则有循环。但是要找到循环的起点,就有些麻烦
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: # @param head, a ListNode # @return a list node def detectCycle(self, head): slow = head fast = head while fast != None and fast.next != None: fast = fast.next.next slow = slow.next if fast != None and slow != None and fast == slow: break if fast == None or fast.next == None: return None slow = head while fast != slow: slow = slow.next fast = fast.next return fast
,具体思路参考
http://www.mysjtu.com/page/M0/S951/951294.html
代码:
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
- 孙悟空:那是一千多年,我们都没有什么经验。 唐三藏:悟空,你可知道我吃的什么肉
- 打印DBUS连接对应的进程的PID和名字
- proc_create的使用方法
- 删除ADT,重新安装ADT
- This is a test
- [leetcode] Linked List Cycle II
- 把二叉树的叶子节点连成一个二叉链表
- qt5.3.0 vs打包完美解决方案
- 有向图是否有环,无向图是否有环
- Codeforces Round #279 (Div. 2) E
- 【BZOJ 1269】 [AHOI2006]文本编辑器editor
- 语言表达的提升
- 【BZOJ 1507】[NOI2003]Editor
- BS与CS的联系与区别