-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathMergeTwoSortedListsII.cpp
43 lines (35 loc) · 1.19 KB
/
MergeTwoSortedListsII.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
/*
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note: You have to modify the array A to contain the merge of A and B. Do not output anything in your code.
TIP: C users, please malloc the result into a new array and return the result.
If the number of elements initialized in A and B are m and n respectively, the resulting size of array A after your code is executed should be m + n
Example :
Input :
A : [1 5 8]
B : [6 9]
Modified A : [1 5 6 8 9]
LINK: https://www.interviewbit.com/problems/merge-two-sorted-lists-ii/
*/
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 n = A.size();
int m = B.size();
int i=0,j=0;
vector<int> v;
while(i<n&&j<m)
{
if(A[i]<=B[j])
v.push_back(A[i++]);
else
v.push_back(B[j++]);
}
while(i<n)
v.push_back(A[i++]);
while(j<m)
v.push_back(B[j++]);
A=v;
}