forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT23.java
More file actions
74 lines (64 loc) · 2.18 KB
/
Copy pathT23.java
File metadata and controls
74 lines (64 loc) · 2.18 KB
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
64
65
66
67
68
69
70
71
72
73
74
package books;
/**
* @program JavaBooks
* @description: 链表中环的入口节点
* @author: mf
* @create: 2019/09/04 14:28
*/
/*
*/
public class T23 {
public static void main(String[] args) {
ListNode listNode1 = new ListNode(1);
ListNode listNode2 = new ListNode(2);
ListNode listNode3 = new ListNode(3);
ListNode listNode4 = new ListNode(4);
ListNode listNode5 = new ListNode(5);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = listNode5;
listNode5.next = listNode3;
ListNode enterNode = findEnterNode(listNode1);
System.out.println(enterNode.value);
}
private static ListNode findEnterNode(ListNode headNode) {
if (headNode == null) return null;
ListNode meetingNode = findMeetingNode(headNode);
if (meetingNode == null) return null; // 无环
// 找环中有几个节点
ListNode tempNode = meetingNode.next;
int ringNum = 1;
while (tempNode != meetingNode) {
tempNode = tempNode.next;
ringNum++;
}
// 设定两个指针,比如p1 p2
// 一开始都在头节点,当p1 跑到ringNum的时候,p2开始移动
// 当p1 == p2 的时候, 就是环入口节点
ListNode enterNode = headNode;
headNode = headNode.next;
int p1 = 1;
while (enterNode != headNode) {
headNode = headNode.next;
p1++;
if (p1 > ringNum) {
enterNode = enterNode.next;
}
}
return enterNode;
}
private static ListNode findMeetingNode(ListNode headNode) {
ListNode slowNode = headNode.next;
if (slowNode == null) return null;
ListNode fastNode = slowNode.next;
while (fastNode != null && slowNode != null) {
if (fastNode == slowNode) return fastNode;
slowNode = slowNode.next;
fastNode = fastNode.next;
if (fastNode != null) fastNode = fastNode.next; // 提高代码的鲁棒性而已
// fastNode = fastNode.next;
}
return null;
}
}