-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSortedLinkedList.c
99 lines (83 loc) · 1.91 KB
/
mergeSortedLinkedList.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
//
// Created by Prince on 06-09-2024.
//
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* link;
};
void addNode(struct Node** head, const int data) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = data;
newNode->link = NULL;
if (*head == NULL) {
*head = newNode;
}
else {
struct Node* temp = *head;
while (temp->link != NULL) {
temp = temp->link;
}
temp->link = newNode;
}
}
struct Node* mergeSortedLinkedList(const struct Node* head1,
const struct Node* head2) {
struct Node* r = NULL;
while (head1 != NULL && head2 != NULL) {
if (head1->data < head2->data) {
addNode(&r, head1->data);
head1 = head1->link;
}
else {
addNode(&r, head2->data);
head2 = head2->link;
}
}
while (head1 != NULL) {
addNode(&r, head1->data);
head1 = head1->link;
}
while (head2 != NULL) {
addNode(&r, head2->data);
head2 = head2->link;
}
return r;
}
void display(const struct Node* head) {
if (head == NULL) {
printf("List is empty\n");
}
else {
const struct Node* temp = head;
while (temp != NULL) {
printf("[%d] ", temp->data);
temp = temp->link;
}
printf("\n");
}
}
int main() {
struct Node* head1 = NULL;
struct Node* head2 = NULL;
int size, data;
printf("\nEnter the initial size of the first linked list: ");
scanf("%d", &size);
for (int i = 0; i < size; i++) {
printf("Enter the value #%d: ", i + 1);
scanf("%d", &data);
addNode(&head1, data);
}
printf("\nEnter the initial size of the second linked list: ");
scanf("%d", &size);
for (int i = 0; i < size; i++) {
printf("Enter the value #%d: ", i + 1);
scanf("%d", &data);
addNode(&head2, data);
}
const struct Node* mergedHead = mergeSortedLinkedList(head1, head2);
printf("\nThe merged Linked List is: ");
display(mergedHead);
return 0;
}