-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntryNodeInListLoop.cpp
More file actions
56 lines (47 loc) · 1.18 KB
/
EntryNodeInListLoop.cpp
File metadata and controls
56 lines (47 loc) · 1.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
// 23 链表中环的入口节点
// TODO: 数学证明
#include "array_util.h"
using namespace std;
ListNode* MeetingNode(ListNode* pHead)
{
if (pHead == nullptr)
return nullptr;
ListNode* pSlow = pHead->next;
if (pSlow == nullptr)
return nullptr;
ListNode* pFast = pSlow->next;
while (pSlow && pFast) {
if (pFast == pSlow)
return pFast;
pSlow = pSlow->next;
pFast = pFast->next;
/// 防止 **NULL** 指针的解引用
if (pFast)
pFast = pFast->next;
}
return nullptr;
}
ListNode* EntryNodeOfLoop(ListNode* pHead)
{
ListNode* meetingNode = MeetingNode(pHead);
if (!meetingNode)
return nullptr;
///得到环中节点的数目
int nodesInLoop = 1;
ListNode* pNode1 = meetingNode;
while (pNode1->next != meetingNode) {
pNode1 = pNode1->next;
++nodesInLoop;
}
/// 先移动一个指针
pNode1 = pHead;
for (int i = 0; i < nodesInLoop; i++) {
pNode1 = pNode1->next;
}
ListNode* pNode2 = pHead;
while (pNode1 != pNode2) {
pNode1 = pNode1->next;
pNode2 = pNode2->next;
}
return pNode1;
}