forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
removeDuplicate.c
91 lines (76 loc) · 1.74 KB
/
removeDuplicate.c
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
/*
A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. It is a
collection of nodes.Nodes consists of 2 parts data part and address part. Address part contains the address of the next or the
successor node. In this program, duplicate elements present in sorted linked list is removed.
*/
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
}*first=NULL,*second=NULL,*third=NULL;
void Display(struct Node *p)
{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
void create(int A[],int n)
{
int i;
struct Node *t,*last;
first=(struct Node *)malloc(sizeof(struct Node));
first->data=A[0];
first->next=NULL;
last=first;
for(i=1;i<n;i++)
{
t=(struct Node*)malloc(sizeof(struct Node));
t->data=A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void removeDuplicate(struct Node *p)
{
struct Node *q=p->next;
while(q!=NULL)
{
if(p->data!=q->data)
{
p=q;
q=q->next;
}
else
{
p->next=q->next;
free(q);
q=p->next;
}
}
}
int main()
{
int A[]={10,20,20,40,50,50,50,60};
clrscr();
printf("Sorted list with no duplicate elements is : ");
create(A,8);
removeDuplicate(first);
Display(first);
getch();
return 0;
}
/*
SAMPLE INPUT OUTPUT
int A[]={10,20,20,40,50,50,50,60}
output:
Sorted list with no duplicate elements is : 10 20 40 50 60
Time Complexity: O(n)
where n is the number of nodes in the given linked list
Space Complexity: O(n)
where n is the number of nodes in the given linked list
*/