-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathring-linked-list.cpp
151 lines (138 loc) · 3.27 KB
/
ring-linked-list.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
基于有环链表的一些问题,整体还是比较简单的。为了描述方便,其实重复了很多代码。
只要我们添加两个指针:入环结点、相遇点。那么代码就能化简很多,这里就不再写了。
配合个人博客食用更佳:
https://blog.csdn.net/iwts_24/article/details/83421853
*/
#include<iostream>
using namespace std;
struct Node {
int val;
struct Node *next;
}head,a,b,c,d,e,f;
// 比较简单明了地创建链表
void create_list() {
a.val = 1;
b.val = 2;
c.val = 3;
d.val = 4;
e.val = 5;
f.val = 6;
head.next = &a;
a.next = &b;
b.next = &c;
c.next = &d;
d.next = &e;
e.next = &f;
f.next = &c;
}
// 测试链表是否正确
void test_link_list() {
Node *temp = head.next;
for (int i = 0; i < 10; i++) {
cout << temp->val << endl;
temp = temp->next;
}
}
// 是否有环,1为存在,0为不存在
int have_ring() {
Node *slow = &head;
Node *fast = &head;
if (fast->next != NULL) fast = fast->next->next;
slow = slow->next;
while (slow != NULL && fast != NULL) {
if (slow == fast) return 1;
slow = slow->next;
if (fast->next == NULL) return 0;
fast = fast->next->next;
}
return 0;
}
// 获得入环结点
Node * get_ring_node() {
Node *p_head = &head;
Node *p_meet;
Node *slow = &head;
Node *fast = &head;
if (fast->next != NULL) fast = fast->next->next;
slow = slow->next;
while (slow != NULL && fast != NULL) {
if (slow == fast){
p_meet = slow;
break;
}
slow = slow->next;
fast = fast->next->next;
}
while (p_head != p_meet) {
p_head = p_head->next;
p_meet = p_meet->next;
}
return p_head;
}
// 获得链表结点总数
int get_length() {
Node *p_head = &head;
Node *p_meet;
Node *slow = &head;
Node *fast = &head;
if (fast->next != NULL) fast = fast->next->next;
slow = slow->next;
while (slow != NULL && fast != NULL) {
if (slow == fast) {
p_meet = slow;
break;
}
slow = slow->next;
fast = fast->next->next;
}
int a = 0;
while (p_head != p_meet) {
p_head = p_head->next;
p_meet = p_meet->next;
a++;
}
int x = 0;
// 此时slow指针是指向相遇点没有变的
while (p_head != slow) {
p_head = p_head->next;
x++;
}
return 2*a + x - 1;
}
// 获得链表环上结点总数
int get_ring_length() {
Node *p_head = &head;
Node *p_meet;
Node *slow = &head;
Node *fast = &head;
if (fast->next != NULL) fast = fast->next->next;
slow = slow->next;
while (slow != NULL && fast != NULL) {
if (slow == fast) {
p_meet = slow;
break;
}
slow = slow->next;
fast = fast->next->next;
}
int a = 0;
while (p_head != p_meet) {
p_head = p_head->next;
p_meet = p_meet->next;
a++;
}
int x = 0;
// 此时slow指针是指向相遇点没有变的
while (p_head != slow) {
p_head = p_head->next;
x++;
}
return a + x;
}
int main() {
create_list();
// test_link_list();
system("pause");
return 0;
}