In: Computer Science
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//ExtLinkedList.java
import java.util.LinkedList;
import java.util.ListIterator;
public class ExtLinkedList<E> extends LinkedList<E> {
// required method
public ExtLinkedList<E> mergeThreeLists(ExtLinkedList<E> list1,
ExtLinkedList<E> list2) {
// if any parameter is null, initializing it to empty list, so that the
// method works without any issues at all. (note: this would not make
// any changes to the original parameters passed to the method)
if (list1 == null) {
list1 = new ExtLinkedList<E>();
}
if (list2 == null) {
list2 = new ExtLinkedList<E>();
}
// creating an ExtLinkedList to store result
ExtLinkedList<E> result = new ExtLinkedList<E>();
// fetching an iterator from this list
ListIterator<E> it = this.listIterator();
// adding each element to result using iterator
while (it.hasNext()) {
result.add(it.next());
}
// repeating this for list1
it = list1.listIterator();
while (it.hasNext()) {
result.add(it.next());
}
// repeating this for list2
it = list2.listIterator();
while (it.hasNext()) {
result.add(it.next());
}
// returning final list
return result;
}
}