-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathL2-002_链表去重.cpp
67 lines (65 loc) · 2 KB
/
L2-002_链表去重.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
#include <bits/stdc++.h>
using namespace std;
// 模拟链表操作,用cnt记录重复数量,isFirst记录是否第一个重复元素
struct node {
int next;
int key;
};
int main(int argc, char const *argv[]) {
unordered_map<int, node> linklist;
unordered_map<int, int> cnt;
unordered_map<int, bool> isFirst;
int head, addr, next;
int n, key;
scanf("%d%d", &head, &n);
while (n--) {
scanf("%d%d%d", &addr, &key, &next);
cnt[abs(key)]++;
linklist[addr] = {next, key};
}
int prevAddr = -1;
int curAddr = head;
int removeHead = -1;
int removeCurAddr = -1;
while (curAddr != -1) {
int val = abs(linklist[curAddr].key);
if (cnt[val] > 1) {
if (!isFirst[val]) {
isFirst[val] = true;
prevAddr = curAddr;
curAddr = linklist[curAddr].next;
} else {
if (removeHead == -1) {
removeHead = curAddr;
removeCurAddr = removeHead;
} else {
linklist[removeCurAddr].next = curAddr;
removeCurAddr = curAddr;
}
linklist[prevAddr].next = linklist[curAddr].next;
curAddr = linklist[curAddr].next;
linklist[removeCurAddr].next = -1;
}
} else {
prevAddr = curAddr;
curAddr = linklist[curAddr].next;
}
}
while (head != -1) {
printf("%05d %d", head, linklist[head].key);
if (linklist[head].next != -1)
printf(" %05d\n", linklist[head].next);
else
printf(" -1\n");
head = linklist[head].next;
}
while (removeHead != -1) {
printf("%05d %d", removeHead, linklist[removeHead].key);
if (linklist[removeHead].next != -1)
printf(" %05d\n", linklist[removeHead].next);
else
printf(" -1\n");
removeHead = linklist[removeHead].next;
}
return 0;
}