Skip to content

Commit 411ef74

Browse files
committed
add LeetCode 142. 环形链表 II
1 parent 8b59803 commit 411ef74

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
6+
7+
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。
8+
9+
说明:不允许修改给定的链表。
10+
11+
进阶:
12+
13+
你是否可以不用额外空间解决此题?
14+
15+
16+
示例 1:
17+
18+
![](https://img-blog.csdnimg.cn/20201010201233916.png#pic_center)
19+
20+
21+
```javascript
22+
输入:head = [3,2,0,-4], pos = 1
23+
输出:返回索引为 1 的链表节点
24+
解释:链表中有一个环,其尾部连接到第二个节点。
25+
```
26+
27+
示例 2:
28+
29+
![](https://img-blog.csdnimg.cn/20201010201245701.png#pic_center)
30+
31+
32+
```javascript
33+
输入:head = [1,2], pos = 0
34+
输出:返回索引为 0 的链表节点
35+
解释:链表中有一个环,其尾部连接到第一个节点。
36+
```
37+
38+
示例 3:
39+
40+
![](https://img-blog.csdnimg.cn/20201010201254500.png#pic_center)
41+
42+
43+
```javascript
44+
输入:head = [1], pos = -1
45+
输出:返回 null
46+
解释:链表中没有环。
47+
```
48+
49+
50+
51+
提示:
52+
53+
- 链表中节点的数目范围在范围 `[0, 104]`
54+
- `-10^5 <= Node.val <= 10^5`
55+
- pos 的值为 -1 或者链表中的一个有效索引
56+
57+
来源:力扣(LeetCode)
58+
链接:https://leetcode-cn.com/problems/linked-list-cycle-ii
59+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
60+
61+
## 解题思路
62+
63+
两个快慢指针,从头节点出发,如果链表有环,快指针肯定可以在环内和慢指针相遇。没有环就不可能再相遇,相遇必在环内。
64+
65+
![](https://img-blog.csdnimg.cn/20201010201733832.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
66+
67+
![](https://img-blog.csdnimg.cn/20201010201755252.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
68+
相遇时,慢指针走的距离:`D+S1D+S1`
69+
70+
假设相遇时快指针已经绕环 n 次,它走的距离:`D+n(S1+S2)+S1D+n(S1+S2)+S1`
71+
72+
因为快指针的速度是 2 倍,所以相同时间走的距离也是 2 倍:
73+
74+
D+n(S1+S2)+S1 = 2(D+S1)
75+
76+
求解得到:**(n-1)S1+ nS2=D**
77+
78+
79+
我们不关心在相遇时快指针已经绕了几次环,我们取 n = 1 ,消掉了 S1:
80+
81+
**D=S2**
82+
83+
那么,当快慢指针第一次相遇时,将快指针放回到头节点,由于 `D=s2`,那么我们快慢指针一起走,都走1步,它们必定会走到入环点,然后相遇,此时就可返回对应指针下标。
84+
85+
```javascript
86+
/**
87+
* Definition for singly-linked list.
88+
* function ListNode(val) {
89+
* this.val = val;
90+
* this.next = null;
91+
* }
92+
*/
93+
94+
/**
95+
* @param {ListNode} head
96+
* @return {ListNode}
97+
*/
98+
var detectCycle = function(head) {
99+
let fast = head, low = head; // 首先,都从头节点出现
100+
while(fast){ // 确保存在环
101+
if(fast.next == null) return null; // fast.next 为null表示无环
102+
low = low.next; // 慢指针走一步
103+
fast = fast.next.next; // 快指针走两步
104+
if(low == fast){ // 初次相遇
105+
fast = head; // 快指针回到头节点
106+
while(true){
107+
if(fast == low){
108+
return low;
109+
}
110+
fast = fast.next; // 快慢指针一起走
111+
low = low.next;
112+
}
113+
}
114+
115+
}
116+
return null;
117+
};
118+
```
119+
参考 <a href="https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/141ti-de-kuo-zhan-ru-guo-lian-biao-you-huan-ru-he-/">笨猪爆破组</a> 图解
120+
121+
## 最后
122+
文章产出不易,还望各位小伙伴们支持一波!
123+
124+
往期精选:
125+
126+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
127+
128+
<a href="https://github.com/Chocolate1999/leetcode-javascript">leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)</a>
129+
130+
小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!
131+
132+
133+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
134+
135+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
136+
137+
```javascript
138+
学如逆水行舟,不进则退
139+
```
140+
141+

0 commit comments

Comments
 (0)