-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMerge2LLSorted.cpp
156 lines (109 loc) · 2.82 KB
/
Merge2LLSorted.cpp
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
#include<iostream>
//PROGRAM TO MERGE TO LISTS IN A SORTED MANNER-
// Merge two lists A and B as one linked list in a sorted manner
using namespace std;
//Node is defined as
struct Node
{
int data;
struct Node *next;
};
//RECURSIVE SOLUTION to MERGING 2 SORTED LINKED LISTS
Node* MergeListsRec(Node *headA, Node* headB)
{
Node *new_head = NULL;
//BASE CONDITIONS
if(headA==NULL) { //if List A is null-our answer is list B
return headB;
}
else if(headB==NULL) {//if List B is null-our answer is list A
return headA;
}
if(headA->data <= headB->data)
{
new_head = headA;
new_head->next= MergeListsRec(headA->next,headB);
}
else {
new_head=headB;
new_head->next = MergeListsRec(headB->next,headA);
}
return(new_head);
}
//-----------------------------SOLUTION USING REFERENCES and ITERATIVE SOLUTION--------------------
Node* MergeList(Node *headA, Node *headB) {
Node *new_head=NULL;
Node *temp; //the ned dummy variable which will keep track of the sorted list
if(headA==NULL) {
return headB;
}
else if(headB==NULL) {
return headA;
}
//if data in node A < data in node B then assign temp to the smaller node
if(headA->data <= headB->data) {
temp=headA;
headA = temp->next; //head A moves to the next of temp
}
else {
temp=headB;
headB=temp->next; //headB moves to next of temp
}
new_head=temp;// now we need to assign new_head to temp node which is the node with smallest value
//this new_head will be returned by the function
while(headA && headB) {
if(headA->data <= headB->data) {
temp->next=headA;
temp=headA;
headA=headA->next;
}
else {
temp->next=headB; //temp links to headB
temp=headB;//temp becomes headB node
headB=headB->next;//headB moves to next to temp
}
}
if(headA==NULL) temp->next=headB; //when List A is finished
if(headB==NULL) temp->next=headA; //when list B is finished
return(new_head); //will return the new header of the sorted merged LL
}
void traverse(Node *head)
{
while(head)
{
cout<<head->data<<" ";
head = head->next;
}
}
int main()
{
//list-A
struct Node *n1 = new Node();
struct Node *n2 = new Node();
struct Node *n3 = new Node();
struct Node *n4 = new Node();
//List B
struct Node *m1 = new Node();
struct Node *m2 = new Node();
struct Node *m3 = new Node();
struct Node *m4 = new Node();
n1->data = 1;
n2->data = 2;
n3->data = 10;
n4->data = 12;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = NULL;
m1->data = 3;
m2->data = 4;
m3->data = 6;
m4->data = 8;
m1->next = m2;
m2->next = m3;
m3->next = m4;
m4->next = NULL;
Node *new_head = MergeListsRec(n1,m1);
traverse(new_head);
return 0;
}