Could somebody please help. Thanks in advance!
Merge two sorted linked lists and return it as a new list.
1) Implement a method: mergeTwoLists(…){…}
2) Use the LinkedList library in Java
3) Test your method.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
import java.util.*;
public class Inclass1 {
public static void main(String[] args){
//Question 5.2
LinkedList l1 = new LinkedList<>();
LinkedList l2 = new LinkedList<>();
l1.add(1);
l1.add(2);
l1.add(10);
l2.add(1);
l2.add(3);
l2.add(4);
LinkedList l3 = mergeTwoLists(l1, l2);
System.out.println("Question \n******************************");
for(int i = 0; i < l1.size(); i++){
System.out.print(l1.get(i) + ((i == l1.size() - 1) ? "" : "->"));
}
System.out.println();
for(int i = 0; i < l2.size(); i++){
System.out.print(l2.get(i) + ((i == l2.size() - 1) ? "" : "->"));
}
System.out.println();
for(int i = 0; i < l3.size(); i++){
System.out.print(l3.get(i) + ((i == l3.size() - 1) ? "" : "->"));
}
}
/* public static LinkedList mergeTwoLists(LinkedList l1, LinkedList l2){
// Inclass assignment
}*/
// Inclass1.java: Java program to merge 2 sorted linked
lists
import java.util.*;
public class Inclass1 {
public static void main(String[] args){
//Question 5.2
LinkedList l1 = new LinkedList<>();
LinkedList l2 = new LinkedList<>();
l1.add(1);
l1.add(2);
l1.add(10);
l2.add(1);
l2.add(3);
l2.add(4);
LinkedList l3 = mergeTwoLists(l1, l2);
System.out.println("Question \n******************************");
for(int i = 0; i < l1.size(); i++){
System.out.print(l1.get(i) + ((i == l1.size() - 1) ? "" :
"->"));
}
System.out.println();
for(int i = 0; i < l2.size(); i++){
System.out.print(l2.get(i) + ((i == l2.size() - 1) ? "" :
"->"));
}
System.out.println();
for(int i = 0; i < l3.size(); i++){
System.out.print(l3.get(i) + ((i == l3.size() - 1) ? "" :
"->"));
}
}
// method to merge and return two sorted linked list of integers
into a single linked list
public static LinkedList mergeTwoLists(LinkedList l1,
LinkedList l2) {
LinkedList merged = new
LinkedList<>(); // create an empty linked list
int i,j;
// loop that continues till the end
of list l1 and l2
for(i=0,j=0;i<l1.size()
&& j < l2.size();)
{
// if ith
element of l1 <= jth element of l2, insert ith element of l1
into merged list and increment i
// cast the
element to Comparable so that both can be compared
if(((Comparable)l1.get(i)).compareTo(l2.get(j)) <=0)
{
merged.add(l1.get(i));
i++;
}
else // if ith
element of l1 > jth element of l2, insert jth element of l2 into
merged list and increment j
{
merged.add(l2.get(j));
j++;
}
}
// if there are elements remaining
in l1, insert them in merged in the order appearing in l1
for(;i<l1.size();i++)
merged.add(l1.get(i));
// if there are elements remaining
in l2, insert them in merged in the order appearing in l2
for(;j<l2.size();j++)
merged.add(l2.get(j));
return merged; // return the merged
list
}
}
//end of Inclass1.java
Output:

Could somebody please help. Thanks in advance! Merge two sorted linked lists and return it as...
Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...
Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...
Hey i got the program to work but i cant get the merge sorter from big to little import java.util.*; class MergeSorter { public static void sort(int[] a) { if (a.length <= 1) { return; } int[] first = new int[a.length / 2]; int[] second = new int[a.length - first.length]; for (int i = 0; i < first.length; i++) { first[i] = a[i]; } for (int i = 0; i < second.length; i++) { second[i] =...
Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...
How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...
Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q { public static LinkedList<String> dogs = new LinkedList<String> (); public static LinkedList<String> cats = new LinkedList<String> (); public static LinkedList<String> animals = new LinkedList<String> (); public static void enqueueCats(){ System.out.println("Enter name"); Scanner sc=new Scanner(System.in); String name = sc.next(); cats.addLast(name); animals.addLast(name); } ...
Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...
Write merge method for mergeSort method, it takes the array of data, starting index of first half, starting index of second half and end index of second half. please use the code below to test it public class A4Sort{ public static void mergeSort(int[] a, int l, int h){ if (l >= h) return; int mid = (h + l) / 2; mergeSort(a, l, mid); mergeSort(a, mid + 1, h); merge(a, l, mid + 1, h); } public static void main(String[]...
For merge sort the time complexity is Θ(nlogn), but what if we had two unsorted stacks and wanted to but merge them into one final sorted stack! what is the time complexity then? code / Java program to merge to unsorted stacks // into a third stack in sorted way. import java.io.*; import java.util.*; public class GFG { // This is the temporary stack static Stack<Integer> res = new Stack<Integer>(); static Stack<Integer> tmpStack = new Stack<Integer>(); //...
Please solve using java.
21. Merge Two Sorted Lists Easy 2705 396 ♡ Favorite Sha * Definition for singly-linked list. * public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. class Solution { public ListNode merge TwoLists(ListNode li, ListNode 12) { 10 Example: Input: 1->2->4, 1-3-4...