Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note:
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
import java.util.Deque;
import java.util.LinkedList;
public class Solution {
public void merge(int A[], int m, int B[], int n) {
Deque<Integer> arr = new LinkedList<Integer>();
int i = 0, j = 0;
for(int k = 0; k < m + n; k++){
if(i > m - 1) arr.push(B[j++]);
else if(j > n - 1) arr.push(A[i++]);
else if(A[i] <= B[j]) arr.push(A[i++]);
else arr.push(B[j++]);
}
int index = m+n-1;
while(!arr.isEmpty())
A[index--] = arr.pop();
}
}