-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopStart56.java
More file actions
121 lines (110 loc) · 3.25 KB
/
LoopStart56.java
File metadata and controls
121 lines (110 loc) · 3.25 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package offer;
import java.util.HashMap;
/**
* 判断链表中环的起始位置 一个链表中包含环,请找出该链表的环的入口结点。
* 1)可以通过hash存储节点判断有没有环的。
* 总结: http://blog.csdn.net/doufei_ccst/article/details/10578315
* http://www.cnblogs.com/xudong-bupt/p/3667729.html
* @author fqx
*
*/
public class LoopStart56 {
/**
* 如果有环,则 返回 快慢指针相遇的节点
* 如果没有环,那么返回null
*
* @param pHead
* @return
*/
public ListNode meetingNode(ListNode pHead) {
return null;
}
public ListNode EntryNodeOfLoop(ListNode pHead) {
//at the start point to Head.
ListNode slow = pHead;
ListNode fast = pHead;
//each loop; slow move one step,fast move two step
while(slow != null && fast.next != null){
//one step
slow = slow.next;
//because the fast.next != null
// so fast.next.next don't call exception
//two step
fast = fast.next.next;
// slow and fast meet with each other
if(slow == fast){
// return fast;
break;
}
}
/**
* 如果要求环长度
* 就是slow 再次碰到 slow时就走过了环长
*/
// return null;
/**
* if there is no loop so ,the fast.next will null
* then this condition will return null/.
*/
if(slow == null || fast.next == null) return null;
/*
* lenA+x =s
* lenA+x + n*R = 2s
*/
ListNode p1 = pHead;
ListNode p2 = slow;
//the entry node of the loop
while(p1 != p2){
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
/**
* 创建一个有环的单链表
*
* @param head
* @return
*/
public static ListNode InsertFromTail(ListNode head) {
// β���뷨
ListNode pre = head;
ListNode joinNode = null;
for (int i = 1; i <= 6; i++) {
ListNode node = new ListNode(i);
if (i == 1) {
head = node;
pre = head;
} else {
pre.next = node;
pre = node;
if (i == 3) {
joinNode = node;
}
}
}
// 环连接点在第三个位置
pre.next = joinNode;
ListNode p = head;
/**
* 此处又重新使用了containsKey:注意equals方法和hashCode()
* 也可以通过这个实现环的判断,以及环的长度等问题。
*/
HashMap<ListNode, Integer> map = new HashMap<ListNode, Integer>();
while (p != null) {
if (!map.containsKey(p)) {
map.put(p, p.val);
} else {
break;
}
p = p.next;
}
return head;
}
public static void main(String[] args) {
ListNode head = null;
head = InsertFromTail(head);
ListNode node = new LoopStart56().EntryNodeOfLoop(head);
System.out.println(node.val);
}
}