Question

JAVA - Write a function in your Main class LinkedList<E> mergeLists(LinkedList<E> list1, LinkedList<E> list2) - given...

JAVA - Write a function in your Main class LinkedList<E> mergeLists(LinkedList<E> list1, LinkedList<E> list2) - given two ordered linked lists of integers, merge the two lists into a single LinkedList whose elements are in sorted order. You should create a new LinkedList and add the values from list1 and list2 into the new list in sorted order. Do not modify list1 or list 2.

  1. list1 => 1 -> 3 -> 7 -> 8, list2 => 2 -> 5 -> 7 -> 10, returns 1 -> 2 -> 3 -> 7 -> 7 -> 8 -> 10
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Arrays;
import java.util.LinkedList;

public class MergeLinkedLists {

    static <E extends Comparable<E>> LinkedList<E> mergeLists(LinkedList<E> list1, LinkedList<E> list2) {
        LinkedList<E> result = new LinkedList<>();
        int i = 0, j = 0;
        while (i <list1.size() || j < list2.size()) {
            if (j >= list2.size() || (i < list1.size() && list1.get(i).compareTo(list2.get(j)) < 0)) {
                result.add(list1.get(i++));
            } else {
                result.add(list2.get(j++));
            }
        }
        return result;
    }

    public static void main(String[] args) {
        LinkedList<Integer> list1 = new LinkedList<>(Arrays.asList(1, 3, 7, 8));
        LinkedList<Integer> list2= new LinkedList<>(Arrays.asList(2, 5, 7, 10));
        System.out.println(mergeLists(list1, list2));
    }
}
Add a comment
Know the answer?
Add Answer to:
JAVA - Write a function in your Main class LinkedList<E> mergeLists(LinkedList<E> list1, LinkedList<E> list2) - given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [2.5pts] Write the function connect(listl, list2, k) that takes two lists, listl and list2, and a...

    [2.5pts] Write the function connect(listl, list2, k) that takes two lists, listl and list2, and a non- negative integer k that is less than or equal to the length of listl. It returns (not prints) a new list containing the first k elements of list1, then all elements of list2, then the remaining elements of list1. Hint: slicing could be helpful here

  • CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three...

    CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three files that implement and use a simple linked list. The code was written in early Java-style using the Object class in order to allow the linked list to be a list of objects of any type. While the code works, it is not type-safe. Refactor the code to use Java Generics. You will need to change the Main class to create a linked list...

  • Code in java Using the template below: public class Lab03 { public static void main(String[] args)...

    Code in java Using the template below: public class Lab03 { public static void main(String[] args) { ArrayList<Integer> list1 = new ArrayList<Integer>(); Collections.addAll(list1, 1, 3, 5, 5 ); ArrayList<Integer> list2 = new ArrayList<Integer>(); Collections.addAll(list2, 3, 7, 3, 2, 4 ); ArrayList<Integer> result1= uniqueUnion(list1,list2); System.out.println(result1); Integer[] array = new Integer[]{ 29, 28, 27, 16, 15, -14, 3, -2, 2}; ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(array)); System.out.printf("The average is: %.2f%n", averagePositive(arrayList)); } // end main public static ArrayList<Integer> uniqueUnion(ArrayList<Integer> list1, ArrayList<Integer> list2) {...

  • In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList)...

    In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList) a) Create a DateTime class 1. Add day, month, year, hours, minutes as attributes 2. Add a constructor and a toString() method 3. Implement the Comparable interface, and add a CompareTo() method 4. Add methods to get and set all attributes. b) Add to MyLinkedList class the following methods: 1. Insert a Node to a particular position in the List 2. Insert a Node...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • JAVA (implementing a collection class) write a completed program (must include MAIN) and straight to the output based on 4. Based on the implementation of ArrayIntlist or ArrayList, write a class Sor...

    JAVA (implementing a collection class) write a completed program (must include MAIN) and straight to the output based on 4. Based on the implementation of ArrayIntlist or ArrayList, write a class SortedIntList or SortedList that provides most of the same operations but maintains its elements in sorted order. When a new value is added to the sorted list rather than appending it to the end of the list, it is placed in the appropriate index to maintain sorted order of...

  • With the main function please using the single linked list code written in the class or...

    With the main function please using the single linked list code written in the class or in the lab, write and test the follow function: Merge() function that takes two lists, each of which is sorted in increasing order, and merges the two together into one list which is in increasing order.

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • Java Given the array, which has been converted to a linked list, I need to invoke...

    Java Given the array, which has been converted to a linked list, I need to invoke a remove method to remove a number from the list. The output for the first array should come out like this Now list1 = 24->18->3-7->null Now list2 = 31->-9->5->21->4->0->8->null Now lsit3 = 5->0->1->null but i keep getting this output Now list1= 12->3->7->null Now list2= -7->null Now list3= 5->0->1->null int arr1[] = {24, 18, 12, 3, 7}; int arr2[] = {31, -9, 5, 21, 4,...

  • Need some help with this java problem, thanks! Failure to submit will result in a ZERO...

    Need some help with this java problem, thanks! Failure to submit will result in a ZERO FOR THIS LAB. NO EXCEPTIONS. Write a program that prints out the lists alter the following methods are called. Method takes an of integers as a parameter. The method moves the minimum value in the list to the front, otherwise preserving the order of the elements. For example, if a variable called list stores the following values: [4. 7, 9, 2. 7, 7. 5....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT