-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path链表中换的入口节点.js
More file actions
43 lines (40 loc) · 1017 Bytes
/
链表中换的入口节点.js
File metadata and controls
43 lines (40 loc) · 1017 Bytes
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
/*
题目描述:
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
解:
红蓝指针,红指针每次走一步,蓝指针每次走两步
红蓝指针相遇的时候,蓝指针比红指针多一圈
此时红指针为环的长度,链表总长-红指针步数 = 环入口
*/
function EntryNodeOfLoop(pHead)
{
let p1 = pHead, p2 = pHead;
let count = 0;
let loopCount = 0;
p1.visit = true;
p1 = p1.next;
p2 = p2.next ? p2.next.next : undefined;
count++;
while(p1 && p2 && p1 != p2){
p1.visit = true;
p1 = p1.next;
p2 = p2.next ? p2.next.next : undefined;
count++;
}
if(!p1 || !p2){
return null;
}
loopCount = count;
while(p1 && !p1.visit){
p1.visit = true;
p1 = p1.next;
count++
}
let entranceCount = count - loopCount;
let p = pHead;
while(entranceCount){
p = p.next;
entranceCount --;
}
return p;
}