-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy path09_RemoveDuplicates_linkedList.c
99 lines (90 loc) · 2.36 KB
/
09_RemoveDuplicates_linkedList.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
90
91
92
93
94
95
96
97
98
99
// program to remove duplicates from a linked list
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
// Method 1 - Brute force - Time complexity: O(n*n), space complexity: O(1)
struct Node *removeDuplicates(struct Node *head)
{
struct Node *curr, *temp, *prev = NULL;
for (temp = head; temp; temp = temp->next)
{
prev = temp;
for (curr = temp->next; curr; curr = curr->next)
{
while (curr && temp->data == curr->data)
{
prev->next = curr->next;
curr = curr->next;
}
if (curr == NULL)
break;
prev = curr;
}
}
return head;
}
// Method 2 - Optimized using hashing- Time complexity: O(n), space complexity: O(n)
struct Node *removeDuplicates2(struct Node *head)
{
int counts[1000] = {0};
struct Node *curr, *prev = NULL;
for (curr = head; curr; curr = curr->next)
{
if (counts[curr->data] >= 1)
{
prev->next = curr->next;
continue;
}
prev = curr;
counts[curr->data]++;
}
return head;
}
struct Node *insertAtHead(struct Node *, int);
void display(struct Node *);
int main()
{
struct Node *head = NULL;
head = insertAtHead(head, 60);
head = insertAtHead(head, 50);
head = insertAtHead(head, 30);
head = insertAtHead(head, 50);
head = insertAtHead(head, 40);
head = insertAtHead(head, 30);
head = insertAtHead(head, 10);
head = insertAtHead(head, 20);
head = insertAtHead(head, 10);
head = insertAtHead(head, 30);
head = insertAtHead(head, 30);
head = insertAtHead(head, 30);
printf("\nLinked list\n");
display(head);
head = removeDuplicates(head);
printf("\nLinked list after removing all duplicates (method 1)\n");
display(head);
head = removeDuplicates2(head);
printf("\nLinked list after removing all duplicates (method 2)\n");
display(head);
return 0;
}
struct Node *insertAtHead(struct Node *head, int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (head)
newNode->next = head;
return newNode;
}
void display(struct Node *head)
{
while (head)
{
printf("%d ", head->data);
head = head->next;
}
}