#include <iostream>
#include <unordered_set>
using namespace std;
struct List {
int data = 0;
List* next = nullptr;
List(int x) : data(x) {}
};
void remove_duplicates(List* head) {
std::unordered_set<int> cache;
cache.insert(head->data);
List* next = head->next;
while (next != nullptr) {
if (cache.count(next->data) > 0) {
head->next = next->next;
delete next;
next = head->next;
} else {
cache.insert(next->data);
head = next;
next = head->next;
}
}
}
int main() {
List* head = new List(1);
List* list = head;
for (int index = 2; index < 10; ++index) {
List* next = new List(index);
if (next->data % 2 == 0) {
next->data = 2;
} else if (next->data % 3 == 0) {
next->data = 3;
}
list->next = next;
list = next;
}
remove_duplicates(head);
while (head != nullptr) {
printf("%d\n", head->data);
head = head->next;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dW5vcmRlcmVkX3NldD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBMaXN0IHsKCWludCBkYXRhID0gMDsKCUxpc3QqIG5leHQgPSBudWxscHRyOwoJTGlzdChpbnQgeCkgOiBkYXRhKHgpIHt9Cn07Cgp2b2lkIHJlbW92ZV9kdXBsaWNhdGVzKExpc3QqIGhlYWQpIHsKCXN0ZDo6dW5vcmRlcmVkX3NldDxpbnQ+IGNhY2hlOwoJY2FjaGUuaW5zZXJ0KGhlYWQtPmRhdGEpOwoJTGlzdCogbmV4dCA9IGhlYWQtPm5leHQ7Cgl3aGlsZSAobmV4dCAhPSBudWxscHRyKSB7CgkJaWYgKGNhY2hlLmNvdW50KG5leHQtPmRhdGEpID4gMCkgewoJCQloZWFkLT5uZXh0ID0gbmV4dC0+bmV4dDsKCQkJZGVsZXRlIG5leHQ7CgkJCW5leHQgPSBoZWFkLT5uZXh0OwoJCX0gZWxzZSB7CgkJCWNhY2hlLmluc2VydChuZXh0LT5kYXRhKTsKCQkJaGVhZCA9IG5leHQ7CgkJCW5leHQgPSBoZWFkLT5uZXh0OwoJCX0KCX0KfQoKaW50IG1haW4oKSB7CglMaXN0KiBoZWFkID0g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