-
Notifications
You must be signed in to change notification settings - Fork 31
/
MergeSortedLists2.java
50 lines (39 loc) · 938 Bytes
/
MergeSortedLists2.java
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
package TwoPointers;
import java.util.ArrayList;
/**
* Author - archit.s
* Date - 14/10/18
* Time - 9:11 PM
*/
public class MergeSortedLists2 {
public void merge(ArrayList<Integer> a, ArrayList<Integer> b) {
int i = 0;
int j = 0;
while(i<a.size() && j<b.size()){
if(a.get(i) >= b.get(j)){
a.add(i, b.get(j));
i++;
j++;
}
else{
i++;
}
}
while (j<b.size()){
a.add(b.get(j));
j++;
}
}
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<Integer>(){{
add(-4);
add(3);
}};
ArrayList<Integer> b = new ArrayList<Integer>(){{
add(-2);
add(-2);
}};
new MergeSortedLists2().merge(a,b);
System.out.println(a);
}
}