Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions Two-Pointers/MergeTwoSortedListsII.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
// https://www.interviewbit.com/problems/merge-two-sorted-lists-ii/
// // https://www.interviewbit.com/problems/merge-two-sorted-lists-ii/

bool checkGreater(int A, int B){
if(A > B){
return true;
}
return false;
}
// bool checkGreater(int A, int B){
// if(A > B){
// return true;
// }
// return false;
// }

// void Solution::merge(vector<int> &A, vector<int> &B) {
// // Do not write main() function.
// // Do not read input, instead use the arguments to the function.
// // Do not print the output, instead return values as specified
// // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
// int i = 0, j = 0;
// while((i < A.size()) && (j < B.size())){
// bool a = checkGreater(A[i], B[j]);
// if(a){
// A.insert(A.begin()+i, B[j]);
// i++;
// j++;
// }
// else{
// i++;
// }
// }
// while(j != B.size()){
// A.push_back(B[j]);
// j++;
// }
// }
void Solution::merge(vector<int> &A, vector<int> &B) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
int i = 0, j = 0;
while((i < A.size()) && (j < B.size())){
bool a = checkGreater(A[i], B[j]);
if(a){
A.insert(A.begin()+i, B[j]);
i++;
j++;
}
else{
i++;
}
}
while(j != B.size()){
A.push_back(B[j]);
j++;
}

for(int i=0;i<B.size();i++){
A.push_back(B[i]);
}
sort(A.begin(),A.end());
}