-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq4p9Sub.java
More file actions
44 lines (38 loc) · 1.36 KB
/
Copy pathq4p9Sub.java
File metadata and controls
44 lines (38 loc) · 1.36 KB
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
import java.util.*;
import java.lang.*;
public class q4p9Sub{
public static void main(String[] args){
int[] A = {1,2,3,4};
int[] B = {5,6,7};
LinkedList<Integer>first = new LinkedList<Integer>();
LinkedList<Integer>second = new LinkedList<Integer>();
for(int i: A){
first.add(i);
}
for(int i: B){
second.add(i);
}
ArrayList<LinkedList<Integer>> ans = new ArrayList<>();
weaved(first, second, ans, new LinkedList<Integer>());
System.out.println(ans);
}
public static void weaved(LinkedList<Integer> first, LinkedList<Integer> second, ArrayList<LinkedList<Integer>> results, LinkedList<Integer> prefix){
if(first.size()==0 || second.size()==0){
LinkedList<Integer> result = (LinkedList<Integer>) prefix.clone();
result.addAll(first);
result.addAll(second);
results.add(result);
return;
}
int headFirst = first.removeFirst();
prefix.addLast(headFirst);
weaved(first, second, results, prefix);
prefix.removeLast();
first.addFirst(headFirst);
int headSecond = second.removeFirst();
prefix.addLast(headSecond);
weaved(first, second, results, prefix);
prefix.removeLast();
second.addFirst(headSecond);
}
}