Skip to content

Commit 7206b28

Browse files
committed
Add solution 142 [Java]
1 parent f464320 commit 7206b28

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
6868
| 127 | [Word Ladder](https://github.com/doocs/leetcode/tree/master/solution/127.Word%20Ladder) | `Breadth-first Search` |
6969
| 130 | [Surrounded Regions](https://github.com/doocs/leetcode/tree/master/solution/130.Surrounded%20Regions) | `Depth-first Search`, `Breadth-first Search`, `Union Find` |
7070
| 137 | [Single Number II](https://github.com/doocs/leetcode/tree/master/solution/137.Single%20Number%20II) | `Bit Manipulation` |
71+
| 142 | [Linked List Cycle II](https://github.com/doocs/leetcode/tree/master/solution/142.Linked%20List%20Cycle%20II) | `Linked List`, `Two Pointers` |
7172
| 144 | [Binary Tree Preorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/144.Binary%20Tree%20Preorder%20Traversal) | `Stack`, `Tree` |
7273
| 150 | [Evaluate Reverse Polish Notation](https://github.com/doocs/leetcode/tree/master/solution/150.Evaluate%20Reverse%20Polish%20Notation) | `Stack` |
7374
| 153 | [Find Minimum in Rotated Sorted Array](https://github.com/doocs/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array) | `Array`, `Binary Search` |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## 环形链表 II
2+
### 题目描述
3+
4+
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 `null`
5+
6+
**说明:**不允许修改给定的链表。
7+
8+
**进阶:**
9+
你是否可以不用额外空间解决此题?
10+
11+
### 解法
12+
13+
利用快慢指针,若快指针为 `null`,则不存在环,若快慢指针相遇,则存在环,此时退出循环,利用 `p1` 指向链表头结点,`p2` 指向快慢指针相遇点,`p1`, `p2` 同时前进,相遇时即为入环的第一个节点。
14+
15+
**证明如下:**
16+
17+
![solution142-slow-fast.png](http://p9ucdlghd.bkt.clouddn.com/solution142-slow-fast.png)
18+
19+
假设链表到入环点距离为 `a`,入环点到快慢指针相遇点距离为 `b`,慢指针行程为`s`,快指针行程是它的 2 倍,相遇时快指针比慢指针多走了 `n * r`圈,即 `2s`。则:
20+
```
21+
① a + b = s
22+
② 2s - s = n * r
23+
-> a + b = n * r
24+
-> a = n * r - b
25+
= (n - 1) * r + r - b
26+
```
27+
`r - b` 为相遇点到入环点的距离。
28+
29+
`p1`, `p2` 同时向前走 `r - b``p2` 到达入环点,而 `p1` 距离入环点还有 `(n - 1) * r`,双方同时走 `(n - 1)` 圈即可相遇,此时相遇点就是入环点!
30+
31+
32+
```java
33+
/**
34+
* Definition for singly-linked list.
35+
* class ListNode {
36+
* int val;
37+
* ListNode next;
38+
* ListNode(int x) {
39+
* val = x;
40+
* next = null;
41+
* }
42+
* }
43+
*/
44+
public class Solution {
45+
public ListNode detectCycle(ListNode head) {
46+
ListNode slow = head;
47+
ListNode fast = head;
48+
boolean hasCycle = false;
49+
while (fast != null && fast.next != null) {
50+
slow = slow.next;
51+
fast = fast.next.next;
52+
if (slow == fast) {
53+
hasCycle = true;
54+
break;
55+
}
56+
}
57+
58+
if (hasCycle) {
59+
ListNode p1 = head;
60+
ListNode p2 = slow;
61+
while (p1 != p2) {
62+
p1 = p1.next;
63+
p2 = p2.next;
64+
}
65+
return p1;
66+
}
67+
return null;
68+
69+
}
70+
}
71+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode detectCycle(ListNode head) {
14+
ListNode slow = head;
15+
ListNode fast = head;
16+
boolean hasCycle = false;
17+
while (fast != null && fast.next != null) {
18+
slow = slow.next;
19+
fast = fast.next.next;
20+
if (slow == fast) {
21+
hasCycle = true;
22+
break;
23+
}
24+
}
25+
26+
if (hasCycle) {
27+
ListNode p1 = head;
28+
ListNode p2 = slow;
29+
while (p1 != p2) {
30+
p1 = p1.next;
31+
p2 = p2.next;
32+
}
33+
return p1;
34+
}
35+
return null;
36+
37+
}
38+
}

0 commit comments

Comments
 (0)