forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merging_two_Linked_List.c
135 lines (123 loc) · 2.91 KB
/
Merging_two_Linked_List.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
/*
This program is about merging two Linked list. Here the user is going to enter
values of first and second linked list.
It will merge the two linked lists into one linked list by comparing the each
and every node values and convert into one linked list.
*/
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
} *first = NULL, *second = NULL, *third = NULL;
void Create(int Array1[], int size) {
int i;
struct Node *temp, *last;
first = (struct Node *)malloc(sizeof(struct Node));
first->data = Array1[0];
first->next = NULL;
last = first;
for (i = 1; i < size; i++) {
temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = Array1[i];
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void Create2(int Array2[], int size) {
int i;
struct Node *temp, *last;
second = (struct Node *)malloc(sizeof(struct Node));
second->data = Array2[0];
second->next = NULL;
last = second;
for (i = 1; i < size; i++) {
temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = Array2[i];
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void Display(struct Node *p) {
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void Merge(struct Node *p, struct Node *q) {
struct Node *last;
/*Merging the two linkedlists and making it as third linkedlist
if the condition satisfies*/
if (p->data < q->data) {
third = last = p;
p = p->next;
third->next = NULL;
} else {
third = last = q;
q = q->next;
third->next = NULL;
}
while (p && q) {
if (p->data < q->data) {
last->next = p;
last = p;
p = p->next;
last->next = NULL;
} else {
last->next = q;
last = q;
q = q->next;
last->next = NULL;
}
}
/* Checking whether any node is left in the first and second linkedlist
if so merging those nodes as well. */
if (p)
last->next = p;
if (q)
last->next = q;
}
int main() {
int *Array1, *Array2;
int size;
printf("Enter the size of the array: \n");
scanf("%d", &size);
Array1 = (int *)malloc(size * sizeof(int));
Array2 = (int *)malloc(size * sizeof(int));
printf("Enter first LL Elements: \n");
for (int i = 0; i < size; i++) {
scanf("%d", &Array1[i]);
}
Create(Array1, size);
printf("Enter second LL Elements: \n");
for (int i = 0; i < size; i++) {
scanf("%d", &Array2[i]);
}
Create2(Array2, size);
Merge(first, second);
Display(third);
return 0;
}
// Output:-
/*
Enter the size of the array:
5
Enter first LL Elements:
2
4
7
8
9
Enter second LL Elements:
1
2
3
4
5
1 2 2 3 4 4 5 7 8 9
Time Complexity:- O(m+n)
Space Complexity:- O(1)
*/