This repository was archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdelete_duplicate_value.cpp
More file actions
89 lines (83 loc) · 1.62 KB
/
delete_duplicate_value.cpp
File metadata and controls
89 lines (83 loc) · 1.62 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
81
82
83
84
85
86
87
88
89
#include <iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct Node
{
int data;
Node *next;
};Node* RemoveDuplicates(Node *head)
{
// This is a "method-only" submission.
// You only need to complete this method.
if(head == NULL || head->next == NULL)
return head;
Node *cur = head;
while(cur->next != NULL)
{
Node *sec = cur->next;
while(sec != NULL && sec->data == cur->data)
sec = sec->next;
if(sec != NULL)
{
Node *del = cur->next;
while(del != sec)
{
Node *_next = del->next;
delete del;
del = _next;
}
}
else if(sec == NULL)
{
Node *del = cur->next;
while(del != NULL)
{
Node *_next = del->next;
delete del;
del = _next;
}
}
cur->next = sec;
cur = sec;
if(cur == NULL)
break;
}
return head;
}void Print(Node *head)
{
bool ok = false;
while(head != NULL)
{
if(ok)cout<<" ";
else ok = true;
cout<<head->data;
head = head->next;
}
cout<<"\n";
}
Node* Insert(Node *head,int x)
{
Node *temp = new Node();
temp->data = x;
temp->next = NULL;
if(head == NULL) return temp;
Node *temp1;
for(temp1 = head;temp1->next!=NULL;temp1= temp1->next);
temp1->next = temp;return head;
}
int main()
{
int t;
cin>>t;
while(t-- >0)
{
Node *A = NULL;
int m;cin>>m;
while(m--){
int x; cin>>x;
A = Insert(A,x);}
A = RemoveDuplicates(A);
Print(A);
}
}