forked from geekxh/hello-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
63 lines (56 loc) · 1.44 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @author Anonymous
* @since 2019/11/22
*/
/*
* public class ListNode { int val; ListNode next = null;
*
* ListNode(int val) { this.val = val; } }
*/
public class Solution {
/**
* 求链表环的入口,若没有环,返回null
*
* @param pHead 链表头
* @return 环的入口点
*/
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null || pHead.next == null) {
return null;
}
ListNode fast = pHead;
ListNode slow = pHead;
boolean flag = false;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (fast == slow) {
flag = true;
break;
}
}
// 快指针与慢指针没有相遇,说明无环,返回 null
if (!flag) {
return null;
}
ListNode cur = slow.next;
// 求出环中结点个数
int cnt = 1;
while (cur != slow) {
cur = cur.next;
++cnt;
}
// 指针p1先走cnt步
ListNode p1 = pHead;
for (int i = 0; i < cnt; ++i) {
p1 = p1.next;
}
// p2指向链表头,然后p1/p2同时走,首次相遇的地方就是环的入口
ListNode p2 = pHead;
while (p1 != p2) {
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
}