Question In chapter 1, you learned how to merge two sorted lists and get a third list which is also sorted. Using the LinkedList implementation (posted to the classroom), write a method named mergeUnsortedLists in an application, which given two unsorted lists listl and list2, merge their nodes together to make one new list list3, by taking nodes alternately between the two lists. For example, if list I contains \( [1,5,3] \) and list2 contains \( [7,13,11,8,10] \) then they will be merged into list 3 which will contain \( [1,7,5,13,3,11,8,10] \). Note that if either list (list I or list2) runs out of nodes, then all the nodes in the other list should be added to list3. Write a main method to test this method in which you output the content of the three lists after the merge operation. The header of the method is given below. Note: Use the class LinkedList posted to the classroom. public static Linkedlist mergeUnsortedi.ists (LinkedList 1ist1, LinkedIist list2) ( Problem 2 Write an application that contains a method called multiply that takes a LinkedL ist 11 and returns a new Linkedl ist 12, of the same length, such that node \( i \) of 1.2 contains the product of the data in 11 's nodes up to and including node \( i \) of list 11 . The header of the method multiply is public static LinkedList multiply(LinkedList 11) ( Write also the main method in which you create the linked list 11 below then call the method multiply and pass to it 11. Finally, display the returned linked list 12. Note: Use the class Linkedl ist posted to the classroom. A linked list is considered a subset of another linked list if all its elements are included in the other. Write an application which contains a boolean method called isSubset that accepts as parameters two linked lists and returns true if one list is a subset of the other and false otherwise. The header of the method isSubset is: public static boolean issubset (Linkedist a, Linkedisist b) I Write also the main method in which you create two linked lists listl and list2 using values entered by the programmer then call the method isSubset. Note: Use the class LinkedList posted to the classroom. Page 1 of 2 Problem 4 Add to the LinkedLi st class a method called swap that swaps the it node with the \( j^{\text {th }} \) one using the header. pub1ic void swap(int i, int j) I For example, if the linked list is \( [22,33,44,55,66,77,88,99] \). then swap \( (3,6) \) will change the linked list to \( [22,33,77,55,66,44,88,99] \). Write an application that contains a main method in which you create a linked list 11 then call the method swap and pass to it 11 . Display the content of the linked list 11 before and after the swap. Note. Use the class Linkedl ist posted to the classroom.

ILAVD4 The Asker · Computer Science

Transcribed Image Text: In chapter 1, you learned how to merge two sorted lists and get a third list which is also sorted. Using the LinkedList implementation (posted to the classroom), write a method named mergeUnsortedLists in an application, which given two unsorted lists listl and list2, merge their nodes together to make one new list list3, by taking nodes alternately between the two lists. For example, if list I contains \( [1,5,3] \) and list2 contains \( [7,13,11,8,10] \) then they will be merged into list 3 which will contain \( [1,7,5,13,3,11,8,10] \). Note that if either list (list I or list2) runs out of nodes, then all the nodes in the other list should be added to list3. Write a main method to test this method in which you output the content of the three lists after the merge operation. The header of the method is given below. Note: Use the class LinkedList posted to the classroom. public static Linkedlist mergeUnsortedi.ists (LinkedList 1ist1, LinkedIist list2) ( Problem 2 Write an application that contains a method called multiply that takes a LinkedL ist 11 and returns a new Linkedl ist 12, of the same length, such that node \( i \) of 1.2 contains the product of the data in 11 's nodes up to and including node \( i \) of list 11 . The header of the method multiply is public static LinkedList multiply(LinkedList 11) ( Write also the main method in which you create the linked list 11 below then call the method multiply and pass to it 11. Finally, display the returned linked list 12. Note: Use the class Linkedl ist posted to the classroom. A linked list is considered a subset of another linked list if all its elements are included in the other. Write an application which contains a boolean method called isSubset that accepts as parameters two linked lists and returns true if one list is a subset of the other and false otherwise. The header of the method isSubset is: public static boolean issubset (Linkedist a, Linkedisist b) I Write also the main method in which you create two linked lists listl and list2 using values entered by the programmer then call the method isSubset. Note: Use the class LinkedList posted to the classroom. Page 1 of 2 Problem 4 Add to the LinkedLi st class a method called swap that swaps the it node with the \( j^{\text {th }} \) one using the header. pub1ic void swap(int i, int j) I For example, if the linked list is \( [22,33,44,55,66,77,88,99] \). then swap \( (3,6) \) will change the linked list to \( [22,33,77,55,66,44,88,99] \). Write an application that contains a main method in which you create a linked list 11 then call the method swap and pass to it 11 . Display the content of the linked list 11 before and after the swap. Note. Use the class Linkedl ist posted to the classroom.
More
Transcribed Image Text: In chapter 1, you learned how to merge two sorted lists and get a third list which is also sorted. Using the LinkedList implementation (posted to the classroom), write a method named mergeUnsortedLists in an application, which given two unsorted lists listl and list2, merge their nodes together to make one new list list3, by taking nodes alternately between the two lists. For example, if list I contains \( [1,5,3] \) and list2 contains \( [7,13,11,8,10] \) then they will be merged into list 3 which will contain \( [1,7,5,13,3,11,8,10] \). Note that if either list (list I or list2) runs out of nodes, then all the nodes in the other list should be added to list3. Write a main method to test this method in which you output the content of the three lists after the merge operation. The header of the method is given below. Note: Use the class LinkedList posted to the classroom. public static Linkedlist mergeUnsortedi.ists (LinkedList 1ist1, LinkedIist list2) ( Problem 2 Write an application that contains a method called multiply that takes a LinkedL ist 11 and returns a new Linkedl ist 12, of the same length, such that node \( i \) of 1.2 contains the product of the data in 11 's nodes up to and including node \( i \) of list 11 . The header of the method multiply is public static LinkedList multiply(LinkedList 11) ( Write also the main method in which you create the linked list 11 below then call the method multiply and pass to it 11. Finally, display the returned linked list 12. Note: Use the class Linkedl ist posted to the classroom. A linked list is considered a subset of another linked list if all its elements are included in the other. Write an application which contains a boolean method called isSubset that accepts as parameters two linked lists and returns true if one list is a subset of the other and false otherwise. The header of the method isSubset is: public static boolean issubset (Linkedist a, Linkedisist b) I Write also the main method in which you create two linked lists listl and list2 using values entered by the programmer then call the method isSubset. Note: Use the class LinkedList posted to the classroom. Page 1 of 2 Problem 4 Add to the LinkedLi st class a method called swap that swaps the it node with the \( j^{\text {th }} \) one using the header. pub1ic void swap(int i, int j) I For example, if the linked list is \( [22,33,44,55,66,77,88,99] \). then swap \( (3,6) \) will change the linked list to \( [22,33,77,55,66,44,88,99] \). Write an application that contains a main method in which you create a linked list 11 then call the method swap and pass to it 11 . Display the content of the linked list 11 before and after the swap. Note. Use the class Linkedl ist posted to the classroom.
Community Answer
0HYQIW

【General guidance】The answer provided below has been developed in a clear step by step manner.Step1/4Problem 1:-The code for the linked list is :-public static LinkedList mergeUnsortedLists(LinkedList list1, LinkedList list2) { LinkedList list3 = new LinkedList(); Iterator iter1 = list1.iterator(); Iterator iter2 = list2.iterator(); while (iter1.hasNext() || iter2.hasNext()) { if (iter1.hasNext()) { list3.add(iter1.next()); } if (iter2.hasNext()) { list3.add(iter2.next()); } } return list3;}// main method for testingpublic static void main(String[] args) { LinkedList list1 = new LinkedList(); LinkedList list2 = new LinkedList(); list1.add(1); list1.add(5); list1.add(3); list2.add(17); list2.add(13); list2.add(11); list2.add(8); list2.add(10); LinkedList list3 = mergeUnsortedLists(list1, list2); System.out.println("list1: " + list1); System.out.println("list2: " + list2); System.out.println("list3: " + list3);}ExplanationOutput :-list1: [1, 5, 3]list2: [17, 13, 11, 8, 10]list3: [1, 17, 5, 13, 3, 11, 8, 10]Explanation:Please refer to solution in this step.Step2/4Problem 2:-The code to multiply two single linked list is :-public static LinkedList multiply(LinkedList list) { LinkedList resultList = new LinkedList(); int product = 1; Iterator iter = list.iterator(); while (iter.hasNext()) { int num = (int) iter.next(); product *= num; resultList.add(product); } return resultList;}// main method for testingpublic static void main(String[] args) { LinkedList list1 = new LinkedList(); list1.add(1); list1.add(2); list1.add(3); list1.add(4); list1.add(5); LinkedList list2 = multiply(list1); System.out.println("list1: " + list1); System.out.println("list2: " + list2);}Output:-list1: [1, 2, 3, 4, 5]list2: [1, 2, 6, 24, 120]Explanation:Please refer to solution in this step.Step3/4Problem 3:-The code for the single linked list in problem 3 is:-public static boolean isSubset(LinkedList a, LinkedList b) { if (a.size() > b.size()) { return false; } Iterator iter = a.iterator(); while (iter.hasNext()) { Object element = iter.next(); if (!b.contains(element)) { return false; } } return true;}// main method for testingpublic static void main(String[] args) { Scanner scanner = new Scanner(System.in); LinkedList list1 = new LinkedList(); LinkedList list2 = new LinkedList(); System.out.print("Enter elements for list1 (comma separated): "); String[] input1 = scanner.nextLine().split(","); for (String s : input1) { list1.add(Integer.parseInt(s.trim())); } System.out.print("Enter elements for list2 (comma separated): "); String[] input2 = scanner.nextLine().split(","); for (String s : input2) { list2.add(Integer.parseInt(s.trim())); } boolean isSubset = isSubset(list1, list2) || isSubset(list2, list1); System.out.println("listis a subset of list2: " + isSu ... See the full answer