What is to be submitted? 1. The complete program 2 .Output of the program Progra
ID: 3547800 • Letter: W
Question
What is to be submitted?
1. The complete program
2 .Output of the program
Program Input
1.file SortedLists.txt
Merging Two Sorted Lists to Produce a Third Sorted
List
1. Write a program that reads as input two sorted lists of integers and merges them to
print the resulting list
Explanation / Answer
public static ArrayList<Integer> mergeMyList(ArrayList<Integer> list1,
ArrayList<Integer> list2)
{
ArrayList<Integer> tempList = null;
int n = list1.size() +list2.size();
int l = list2.size();
if ( n == 0 && l == 0)
{
tempList = list1;
return tempList;
}
if ( n == 0 )
{
tempList = list2;
return tempList;
}
if ( l == 0)
{
tempList = list1;
return tempList;
}
else
{
int x = list1.get(0);
int y = list2.get(0);
if (x < y )
{
// list1.add(x);
// list1.add(y);
tempList=list1;
// list1.remove(0);
// list2.remove(0);
}
else
{
list1.add(y);
tempList = list1;
list1.remove(0);
list2.remove(0);
tempList = mergeMyList(list1,list2);
}
}
tempList = mergeMyList(list1,list2);
return tempList;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.