forked from EasonLeeee/TargetOffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht56_链表中环的入口节点.java
62 lines (55 loc) · 1.46 KB
/
t56_链表中环的入口节点.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
/**
* 一个链表中包含环,请找出该链表的环的入口结点。
*/
/**
* 特殊输入:1.无环 2.整个链表为一个环
*/
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead)
{
if (pHead == null)
return null;
if (pHead.next == pHead)
return pHead;
//寻找环中的一个节点
ListNode node = nodeInLoop(pHead);
if (node == null)
return null;
//计算环中元素个数
ListNode node1 = node.next;
int count = 1;
while (node1 != node) {
count++;
node1 = node1.next;
}
//寻找入口
ListNode ahead = pHead;
ListNode behind = pHead;
for (int i = 0; i < count; i++) {
ahead = ahead.next;
}
while (ahead != behind) {
ahead = ahead.next;
behind = behind.next;
}
return ahead;
}
//通过一快一慢两个指针寻找环中的一个节点,无环时返回null
public ListNode nodeInLoop(ListNode pHead) {
if (pHead == null || pHead.next == null) {
return null;
}
ListNode slow = pHead;
ListNode fast = pHead;
slow = slow.next;
fast = fast.next.next;
while (fast != null && fast.next != null && slow != fast) {
slow = slow.next;
fast = fast.next.next;
}
if (fast == slow)
return fast;
else
return null;
}
}