LeetCode——Linked List Cycle II
来源:互联网 发布:美橙互联域名购买多久 编辑:程序博客网 时间:2024/05/16 23:54
若同时从一个环的某个点出发,fast指针每次走两步,slow指针每次走一步,则可证明slow指针走回起点时,fast指针也恰好到达起点。两种思路:一、fast指针每次比slow指针多走一步,fast指针想与slow指针同时到达某点,必须超过slow指针一圈,每次多走一步,假设环的节点数为N,则多走一圈需要走N次,此时slow指针恰好走完一圈指向出发点,fast指针亦然;二、fast指针的速度是slow的两倍,slow走k步,fast就走了2k步,fast指针要超过slow指针一圈即N步,即2k-k=N,一个很简单的方程,得k=N,所以slow指针走N步回到出发点时,fast和slow指针再次指向同一点。
那该怎么求解本题呢?设slow指针到达环的起始点要走k步,则fast指针此时已走了2k步,按照前述,fast指针想“赶上”slow指针需要走N-k次,此时fast和slow指针位于距环的起始点N-k步的位置,但N-k是指从起始点到这个点的步数,我们不能往回走,只能往前走,所有想再次回到起始点,必须再走N-(N-k)即k步。于是思路就很清晰了:先找出fast和slow指针“重合”即相等的地方,此时令slow = head,slow和fast每次只走一步,当走了k步(这个值不重要,只须判断fast和slow是否相等即可)fast和slow指针指向同一个节点使,这个节点即是环的起始点。若不存在这样的点,返回空指针。
<pre name="code" class="cpp">/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *detectCycle(ListNode *head) { ListNode *slow = head; ListNode *fast = head; while(fast && fast->next){ slow = slow->next; fast = fast->next->next; if (slow == fast){ slow = head; while (slow != fast){ slow = slow->next; fast = fast->next; } return slow; } } return NULL; }};
0 0
- LeetCode——Linked List Cycle II
- LeetCode——Linked List Cycle II
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- [LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- LeetCode——141. Linked List Cycle && 142. Linked List Cycle II
- leetcode——142——Linked List Cycle II
- leetcode 142 —— Linked List Cycle II
- LeetCode题解——Linked List Cycle II
- leetcode刷题日记—— Linked List Cycle II
- Leetcode——142. Linked List Cycle II
- LeetCode — Linked List Cycle II 解题报告
- LeetCode 之 Linked List Cycle I II — C++ 实现
- 【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
- 生命的内涵意义!
- <eclipse>tomcat的配置
- Sublime Text 3 常用插件以及安装方法
- vsftp 建立虚拟用户服务器
- elasticsearch插件开发
- LeetCode——Linked List Cycle II
- HDOJ 剪花布条 2087【KMP】
- 区间覆盖,选点
- VW输入文件格式
- 安卓开发时的各种单位问题
- windows程序设计——飞机大战笔记(Access数据库的使用)
- Android官方技术文档翻译——Gradle 插件用户指南(1-3)
- 2012年5月SAT香港真题解析
- 解压、重命名、删除压缩包工具类