-
Notifications
You must be signed in to change notification settings - Fork 24
/
47_merge2SortedList-inPlace_linkedList.c
147 lines (131 loc) · 3.18 KB
/
47_merge2SortedList-inPlace_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
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
// program to Merge 2 sorted lists in O(1) space. (without using extra linked list)
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
// Time complexity: O(m+n), Space complexity: O(1), since no extra list is created
struct Node *mergeListsInPlace(struct Node *list1, struct Node *list2)
{
struct Node *head3 = NULL, *curr = NULL;
if (!list1)
return list2;
if (!list2)
return list1;
if (list1->data <= list2->data)
{
curr = list1;
list1 = curr->next;
}
else
{
curr = list2;
list2 = list2->next;
}
head3 = curr;
while (list1 && list2)
{
if (list1->data <= list2->data)
{
curr->next = list1;
curr = list1;
list1 = list1->next;
}
else
{
curr->next = list2;
curr = list2;
list2 = list2->next;
}
}
if (list1)
curr->next = list1;
if (list2)
curr->next = list2;
return head3;
}
// Slight modification of above, makes code shorter and simpler
struct Node *mergeListsInPlace2(struct Node *list1, struct Node *list2)
{
if (!list1)
return list2;
if (!list2)
return list1;
struct Node *dummyHead = (struct Node *)malloc(sizeof(struct Node));
struct Node *curr = dummyHead;
while (list1 && list2)
{
if (list1->data <= list2->data)
{
curr->next = list1;
curr = list1;
list1 = list1->next;
}
else
{
curr->next = list2;
curr = list2;
list2 = list2->next;
}
}
if (list1)
curr->next = list1;
if (list2)
curr->next = list2;
return dummyHead->next;
}
struct Node *insertInSortedList(struct Node *, int);
void display(struct Node *);
int main()
{
struct Node *head1 = NULL, *head2 = NULL;
head1 = insertInSortedList(head1, 10);
head1 = insertInSortedList(head1, 20);
head1 = insertInSortedList(head1, 30);
head1 = insertInSortedList(head1, 40);
head2 = insertInSortedList(head2, 12);
head2 = insertInSortedList(head2, 23);
head2 = insertInSortedList(head2, 25);
head2 = insertInSortedList(head2, 35);
head2 = insertInSortedList(head2, 45);
printf("\nLinked list 1\n");
display(head1);
printf("\nLinked list 2\n");
display(head2);
head1 = mergeListsInPlace(head1, head2);
printf("\nList 1 after merging list 2\n");
display(head1);
return 0;
}
struct Node *insertInSortedList(struct Node *head, int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (head == NULL)
return newNode;
if (data < head->data)
{
newNode->next = head;
return newNode;
}
struct Node *curr = head, *prev = NULL;
while (curr && data > curr->data)
{
prev = curr;
curr = curr->next;
}
prev->next = newNode;
newNode->next = curr;
return head;
}
void display(struct Node *head)
{
while (head)
{
printf("%d ", head->data);
head = head->next;
}
}