[leetcode] 142. Linked List Cycle II 解题报告
来源:互联网 发布:ai for mac dmg格式 编辑:程序博客网 时间:2024/04/29 21:36
题目链接:https://leetcode.com/problems/linked-list-cycle-ii/
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
思路:本题可以使用快慢指针来解决,快指针一次走两步,慢指针一次走一步。有两个步骤:
1.先找出是否有环及其快慢指针相遇的位置。
2.然后再找出环的入口
先来看第一个怎么解决:
快慢指针初始都在头部,设链表长度为n。
1.假如没有环,那么快指针最终会指向空指针。
2.如果有环,那么慢指针最多走n步(即回到头部)就会和快指针相遇。
如果快慢指针最终相遇了,就说明有环。那么如果找出环的入口位置呢?盗个图(自己也画了一个,嫌太丑)
假如从链表头到环入口长度为k(即D位置),快慢指针相遇在E,从相遇的节点到环入口长度为y。在相遇的时候快指针多走了一圈环形,那么快慢指针到相遇的时候各走的步数为:
slow = k + x;
fast = k + x + y + x;
因为快指针速度比慢指针快一倍,因此有如下关系:fast = 2 * slow,即:(k + x + y +x) = 2 * (k + x),可以得出 k = y。因此现在从快慢指针相遇的地方到环入口和从链表头到环入口的距离相等,所以让快慢指针以相同的速度从这两个地方再开始走,到相遇的地方即环的入口。
代码如下:
/** * 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) { if(!head || !head->next) return NULL; ListNode *slow = head->next, *fast = head->next->next; while(slow!=fast && fast && fast->next) { slow = slow->next; fast = fast->next->next; } if(slow != fast) return NULL; fast = head; while(fast!=slow) fast = fast->next, slow = slow->next; return slow; }};
- [leetcode] 142. Linked List Cycle II 解题报告
- [Leetcode] 142. Linked List Cycle II 解题报告
- [leetcode]142. Linked List Cycle II@Java解题报告
- LeetCode — Linked List Cycle II 解题报告
- [LeetCode OJ] Linked List Cycle II 解题报告
- LeetCode---Linked List Cycle、Linked List Cycle II解题分析
- LeetCode — Linked List Cycle 解题报告
- [LeetCode OJ] Linked List Cycle 解题报告
- [leetcode] 141. Linked List Cycle 解题报告
- 【LeetCode】Linked List Cycle 解题报告
- [Leetcode] 141. Linked List Cycle 解题报告
- [LeetCode]141. Linked List Cycle【&142. Linked List Cycle II】
- leetcode--141. Linked List Cycle&&142. Linked List Cycle II
- LeetCode 141. Linked List Cycle && 142. Linked List Cycle II
- [LeetCode]Reverse Linked List II, 解题报告
- [LeetCode]Reverse Linked List II解题报告
- 【LeetCode】Reverse Linked List II 解题报告
- [LeetCode] Reverse Linked List II 解题报告
- zabbix特性
- mysql 数据库表内容插入新表
- android 中 EditText加入图标 更改边框颜色 设置透明 代码
- java GridLayout布局 网格布局演示
- FBX文件简述
- [leetcode] 142. Linked List Cycle II 解题报告
- mac下,redis的安装与配置
- OpenGL FAQ
- java中多种布局管理器的混合使用
- 使用MySQL Migration Toolkit快速将Oracle数据导入MySQL
- [IMX6Q]u-boot环境变量原理分析
- MyBatis的foreach语句详解
- Git与TortoiseGit基本操作
- 移动平台对 META 标签的定义