-
Notifications
You must be signed in to change notification settings - Fork 0
/
ll_problem_merge_two_sorted_lists.c
198 lines (164 loc) · 4.3 KB
/
ll_problem_merge_two_sorted_lists.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#include <stdlib.h>
/*
This is a solution to merging two sorted linked lists, by creating a new linked list.
Original lists are not modified.
Duplicates in the new list are avoided.
*/
typedef struct Node
{
int data;
struct Node* next;
}Node;
void create_list( Node** ptrHead, int arr[], int arr_size )
{
int i=0;
Node* temp = NULL;
if( arr_size < 1) //invalid array size
{
printf("\nInvalid array size %d\n",arr_size);
return;
}
for( i=arr_size-1; i>=0; i-- )
{
temp = (Node*)malloc(sizeof(Node));
temp->data = arr[i];
temp->next = (*ptrHead);
(*ptrHead) = temp;
}
}
void print_linked_list( Node* head )
{
if( head == NULL )
{
return;
}
else
{
printf("%d\t",head->data);
print_linked_list( head->next );
}
}
void merge_sorted_lists(Node** ptrNewlist, Node* list1, Node* list2 )
{
Node *temp = NULL, *end = NULL;
*ptrNewlist = NULL;
while( list1 !=NULL && list2 !=NULL )
{
/*START : logic to skip duplicates from source list and new list*/
if( end != NULL )
{
while( (list1 != NULL) && (end->data == list1->data ) )
{
list1 = list1->next;
}
while( (list2 != NULL ) && (end->data == list2->data) )
{
list2 = list2->next;
}
}
if( list1 == NULL || list2 == NULL )
{
break;
}
/*END : logic to skip duplicates from source list and new list*/
temp = (Node*)malloc(sizeof(Node));
if( list1->data < list2 ->data )
{
temp->data = list1->data;
list1 = list1->next;
}
else
{
temp->data = list2->data;
list2 = list2->next;
}
temp->next = NULL;
if( *ptrNewlist == NULL )
{
*ptrNewlist = temp;
end = temp;
}
else
{
end->next = temp;
end = temp;
}
}
if( list1 == NULL )
{
//copy rest of list2 to new_list
while( list2 !=NULL )
{
if( end->data == list2->data )
{
list2 = list2->next;
}
else
{
temp = (Node*) malloc(sizeof (Node));
temp->data = list2->data;
temp->next = NULL;
if( *ptrNewlist == NULL )
{
*ptrNewlist = temp;
end = temp;
}
else
{
end->next = temp;
end = temp;
}
list2 = list2->next;
}
}
}
if( list2 == NULL )
{
//copy reset of list1 to new_list
while( list1 !=NULL )
{
if( end->data == list1->data )
{
list1 = list1->next;
}
else
{
temp = (Node*) malloc(sizeof (Node));
temp->data = list1->data;
temp->next = NULL;
if( *ptrNewlist == NULL )
{
*ptrNewlist = temp;
end = temp;
}
else
{
end->next = temp;
end = temp;
}
list1 = list1->next;
}
}
}
}
int main(void)
{
Node *head1 = NULL, *head2 = NULL , *new_list = NULL;
int sorted_arr1[] = {80};//{6,6,6}; //{ 11,17,25,29,31,59,102, 115, 167 };
int sorted_arr2[] = { 6,17,29,45,59,59,59 };
int sorted_arr1_size = ( sizeof( sorted_arr1 ) / sizeof(int) );
int sorted_arr2_size = ( sizeof( sorted_arr2 ) / sizeof(int) );
create_list( &head1, sorted_arr1, sorted_arr1_size );
create_list( &head2, sorted_arr2, sorted_arr2_size );
merge_sorted_lists( &new_list , head1 , head2 );
printf("\nThe sorted linked list-1 is : \n");
print_linked_list( head1);
printf("\n\nThe sorted linked list-2 is : \n");
print_linked_list( head2);
printf("\n\nThe merged list is : \n");
print_linked_list( new_list );
printf("\n\n");
return 0;
}
//end of file