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.
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));
}
}
JAVA - Write a function in your Main class LinkedList<E> mergeLists(LinkedList<E> list1, LinkedList<E> list2) - given...
[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 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) {
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) {...
Write a method called alternate that accepts two Lists as its parameters and returns a new List containing alternating elements from the two lists, in the following order: • First element from first list • First element from second list • Second element from first list • Second element from second list • Third element from first list • Third element from second list If the lists do not contain the same number of elements, the remaining elements from the...
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...
Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...
public class LinkedList { // The LinkedList Node class private class Node{ int data; Node next; Node(int gdata) { this.data = gdata; this.next = null; } } // The LinkedList fields Node head; // Constructor LinkedList(int gdata) { this.head = new Node(gdata); }...
*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 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...
Python. Write a function swap_lists that takes in two lists and swap their elements in place. You can not use another list. You can not return lists. Input lists have the same size. def swap_lists(alist1, alist2): """Swaps content of two lists. Does not return anything. >>> list1 = [1, 2] >>> list2 = [3, 4] >>> swap_lists(list1, list2) >>> print(list1) [3, 4] >>> print(list2) [1, 2] >>> list1 = [4, 2, 6, 8, 90, 45] >>> list2 = [30, 41,...