[LeetCode 题解]: Linked List Cycle II
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?
题意:
给定一个链表,找到环起始的位置。如果环不存在,返回NULL。
分析:
(1)首先要判断该链表是否有环。如果没有环,那么返回NULL。
(2)其次,当已知环存在后,寻找环起始的位置。
思路:
(1)链表是否有环,利用快慢指针很好解决。
(2)当环存在时,寻找环的起始位置:
++++使用额外的空间,可以用一个map存放链表的节点,遍历链表:
如果当前节点在map中没有出现,那么将该节点加入到map中,否则,返回当前节点(也就是环的起点)。
-----不使用额外空间,利用快慢指针的特性。
- 1st ruond:
- SP(start point) MP(match point)
- A: --------------------------------------------------------MP
- B: - - - - - - - - - - - - - MP
- 2nd round:
- B: MP- - - - - -- - - - - - - - -MP
- C:---------------------------------------------------------MP
3rd round:- B1:- - - - - - - - - - - - - MP
- B2: MP- - - - - - - - - - - - - - MP
- B1与B2必定会在途中相遇。 那么,他们首次相遇的点即为环的起始点。
设定两个指针A,B都从SP(Start point)出发: A每次向后移动两个节点,B每次向后移动一个节点。
假设A,B在MP(match point)相遇。 那么A所走的路径长度为B所走的路径长度的2倍:La = 2*Lb。
假设有另一个指针C还是从SP启动,每次向后移动两个节点, B从MP开始出发每次向后移动一个节点,那么可以想象C和B会在MP相遇。
如果C从SP出发,每次只移动一个节点,B依旧从MP出发且每次向后移动一个节点, 那么B和C依旧会在MP相遇。
- class Solution {
- public:
- ListNode *detectCycle(ListNode *head) {
- // list length less than two
- if(head==NULL || head->next==NULL) return NULL;
- // if cycle exists in the list
- ListNode *startA= head->next;
- ListNode *endA= head->next->next;
- while(endA!=NULL && endA != startA){
- endA=endA->next;
- if(endA){
- endA=endA->next;
- startA=startA->next;
- }
- }
- // no cycle exists
- if(endA==NULL) return NULL;
- // find a cycle then search the start point
- ListNode *match = startA;
- ListNode *start = head;
- while(start!=match){
- start = start->next;
- match = match->next;
- }
- return start;
- }
- };
[LeetCode 题解]: Linked List Cycle II的更多相关文章
- [Leetcode Week6]Linked List Cycle II
Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...
- 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】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 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【题解】【链表】【Leetcode】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 单链表中的环之二
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
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- 【leetcode】Linked List Cycle II (middle)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- python命令行中import caffe报错的解决方案
1.ImportError: No module named skimage.io >>> import caffe Traceback (most recent call last ...
- SmallLocks
folly/SmallLocks.h This module is currently x64 only. This header defines two very small mutex types ...
- python的requests模块
使用python进行接口测试得时候可以使用requests模块,是基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库 安装requests是模块 pip instal ...
- C# 二元一次方程参数求解
本文记录了关于求直线斜率及纵截距值的简单方法,只是简单的记录下求解思路,最终还需根据具体项目进行优化. 设直线方程式为:y=kx+b 编程思想: 1.代入y1与x1的值,得到:y1=kx1+b 2.代 ...
- VS2017连接Oracle设置
1. 下载安装 Oracle Developer Tools for Visual Studio 2017: 2. 配置tnsnames.ora ODP.Net默认使用安装目录下的 tnsnames. ...
- MyEclipse8.6启动后提示内存不足的解决方案(亲测,完美解决)
转自:http://www.bubuko.com/infodetail-1625857.html 最近可能由于公司项目大了,启动MyEclipse后经常提示内存不足的警告框,如下: 其实点击close ...
- SC命令安装window服务
sc create Styler binpath= "D:\Styler\Styler.exe" start= auto displayname= "Styler&quo ...
- javascript中defer的作用
javascript中defer的作用 <script src="../CGI-bin/delscript.js" defer></script>中的def ...
- javascript中所谓的“坑”收录
坑一: // 反例myname = "global"; // 全局变量function func() { alert(myname); // "undefined&quo ...
- Node.js究竟是什么?
来源:https://www.ibm.com/developerworks/cn/opensource/os-nodejs/index.html?ca=drs#ibm-pcon Node 旨在解决 ...