-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.1(removeDup).cpp
More file actions
80 lines (73 loc) · 1.35 KB
/
2.1(removeDup).cpp
File metadata and controls
80 lines (73 loc) · 1.35 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
//#include <unordered_set>
using namespace std;
class linkedlist{
public:
struct Node{
int data;
Node *next;
};
linkedlist (){
head = NULL;
}
void insert(int data){
Node *n = new Node();
n->data =data;
n->next = head;
head = n;
}
void display(){
Node *temp = new Node();
temp = head;
while(temp->next != NULL){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<temp->data;
}
/*
//extra buffer -- works with C++ 11 or above because of unordered_set
void removeDup(){
Node *temp = new Node();
temp = head;
unordered_set<int> myset(5);
while(temp->next!= NULL){
myset.insert(temp->data);
temp=temp->next;
}
myset.insert(temp->data);
unoredered_set<int>::iterator it;
for (it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
}
*/ //no extra buffer
void removeDup(){
Node *temp = new Node();
temp = head;
while(temp->next!= NULL){
Node *runner = new Node();
runner = temp;
while(runner->next!=NULL){
if(runner->next->data==temp->data){
runner->next= runner->next->next;
}else{
runner= runner->next;
}
}
temp=temp->next;
}
temp->data;
}
private:
Node *head;
};
int main(){
linkedlist list;
list.insert(5);
list.insert(10);
list.insert(5);
list.insert(8);
list.insert(9);
list.removeDup();
list.display();
}