-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelete_The_DuplicateFrom_LL.cpp
More file actions
100 lines (86 loc) · 1.7 KB
/
Delete_The_DuplicateFrom_LL.cpp
File metadata and controls
100 lines (86 loc) · 1.7 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
90
91
92
93
94
95
96
97
98
99
100
#include <bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node* next;
node(int d)
{
data=d;
next=NULL;
}
};
void InsertAtTail(node* &head,node* &tail,int data)
{
if(head==NULL)
{
node* n=new node(data);
head=tail=n;
}
else
{
node* n=new node(data);
tail->next=n;
tail=n;
}
}
void InputData(node*&head,node*&tail,int n)
{
int data;
while(n--)
{
cin>>data;
InsertAtTail(head,tail,data);
}
}
void removeDuplicates(node* head)
{
/* Pointer to traverse the linked list */
node* current = head;
/* Pointer to store the next pointer of a node to be deleted*/
node* next_next;
/* do nothing if the list is empty */
if (current == NULL)
return;
/* Traverse the list till last node */
while (current->next != NULL)
{
/* Compare current node with next node */
if (current->data == current->next->data)
{
/* The sequence of steps is important*/
next_next = current->next->next;
delete(current->next);
current->next = next_next;
}
else /* This is tricky: only advance if no deletion */
{
current = current->next;
}
}
}
void print(node* head)
{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
node* head=NULL;
node* tail=NULL;
int n;
cin>>n;
InputData(head,tail,n);
removeDuplicates(head);
print(head);
}
return 0;
}