forked from tanus786/CP-Codes-HackOctober-Fest-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Removeduplicates.cpp
57 lines (57 loc) · 1.1 KB
/
Removeduplicates.cpp
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<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
void removeDuplicates(struct Node* head)
{
struct Node* c = head;
struct Node* next_next;
if (c == NULL)
return;
while (c->next != NULL)
{
if (c->data == c->next->data)
{
next_next = c->next->next;
free(c->next);
c->next = next_next;
}
else
{
c = c->next;
}
}
}
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node *node)
{
while (node!=NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 4);
push(&head, 3);
push(&head, 3);
push(&head, 2);
push(&head, 2);
push(&head, 2);
printf("\n Linked list before removing duplicates ");
printList(head);
removeDuplicates(head);
printf("\n Linked list after removing duplicates ");
printList(head);
}