-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_list_dups.cpp
More file actions
59 lines (50 loc) · 856 Bytes
/
Copy pathremove_list_dups.cpp
File metadata and controls
59 lines (50 loc) · 856 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <string>
#include <map>
#include <set>
using std::map;
using std::set;
class Node {
int data;
Node *next;
public:
void print() {
printf("%d ", data);
if(next)
next->print();
}
void removeDups() {
set<int> elts;
Node *cur = this->next;
Node *prev = this;
elts.insert(data);
while(cur) {
if(elts.find(cur->data) != elts.end()) {
prev->next = cur->next;
cur->next = NULL;
delete cur;
}
else {
elts.insert(cur->data);
prev = cur;
}
cur = prev->next;
}
}
Node(int myData, Node *nextNode) : data(myData), next(nextNode) {
}
~Node() {
if(next) {
delete next;
}
}
};
int main() {
Node *head = new Node(1, new Node(2, new Node(2, new Node(3, NULL))));
head->print();
printf("\n");
head->removeDups();
head->print();
printf("\n");
delete head;
}