-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeLists.c
73 lines (61 loc) · 1.47 KB
/
mergeLists.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
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int a;
struct ListNode *next;
};
void createList(struct ListNode **head, int data) {
struct ListNode *temp = (struct ListNode *)malloc(sizeof(struct ListNode *));
temp->a = data;
if(*head == NULL) {
temp->next = NULL;
*head = temp;
} else {
temp->next = *head;
*head = temp;
}
}
struct ListNode *mergeList(struct ListNode *list1, struct ListNode *list2){
/*struct ListNode *sortedList = NULL;
if(list1 == NULL) {
return list2;
}
else if(list2 == NULL) {
return list1;
}
if(list2->a <= list1->a) {
sortedList = list2;
sortedList->next = mergeList(list1, list2->next);
} else {
sortedList = list1;
sortedList->next = mergeList(list1->next, list2);
}
return sortedList;*/
return (list1 == NULL) ? list2 :
(list2 == NULL) ? list1 :
(list1->a < list2->a) ?
(list1->next = mergeList(list1->next, list2), list1) :
(list2->next = mergeList(list2->next, list1), list2);
}
void display(struct ListNode *list) {
while(list != NULL) {
printf("%d->", list->a);
list = list->next;
}
printf("\n");
}
int main() {
struct ListNode *head1 = NULL;
struct ListNode *head2 = NULL;
struct ListNode *sortedList = NULL;
createList(&head1, 1);
createList(&head1, 2);
createList(&head1, 4);
display(head1);
createList(&head2, 1);
createList(&head2, 3);
createList(&head2, 4);
display(head1);
sortedList = mergeList(head1, head2);
display(sortedList);
}