Home / Expert Answers / Computer Science / public-class-assignment-6-public-static-node-computethemergenode-linkedlist-lst1-linkedlist-lst2-pa342

(Solved): public class Assignment_6 {public static Node computeTheMergeNode(LinkedList lst1, LinkedList lst2) ...



student submitted image, transcription available below

public class Assignment_6 {
public static Node computeTheMergeNode(LinkedList lst1, LinkedList lst2){
// Implement this function
return (null);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Random random = new Random();
int szList1UntilMerge = random.nextInt(100) + 1;
int szList2UntilMerge = random.nextInt(100) + 1;
int szListAfterMerge = random.nextInt(100) + 1;
LinkedList list1 = new LinkedList();
LinkedList list2 = new LinkedList();
for (int i=0;i<szList1UntilMerge;i++){
int value = random.nextInt(1000);
list1.insert(value);
}
if (list1.count() != szList1UntilMerge){
System.out.print("There is an error in the insert function!");
}
Node mergeNode = list1.insert(2000+random.nextInt(100));
for (int i=0;i<szListAfterMerge;i++){
int value = mergeNode.mValue + 1 + random.nextInt(1000);
list1.insert(value);
}
if (list1.count() != szList1UntilMerge+szListAfterMerge+1){
System.out.print("There is an error in the insert function!");
}
for (int i=0;i<szList2UntilMerge;i++){
int value = random.nextInt(1000);
list2.insert(value);
}
if (list2.count() != szList2UntilMerge){
System.out.print("There is an error in the insert function!");
}
Node curr = list2.mHead;
Node prev = null;
while (curr != null){
prev = curr;
curr = curr.mNext;
}
prev.mNext = mergeNode;

if (list2.count() != szList2UntilMerge+szListAfterMerge+1){
System.out.print("There is an error in the merge function!");
}
System.out.println("List 1:");
list1.print();
System.out.println("List 2:");
list2.print();
Node mergeNode2 = computeTheMergeNode(list1, list2);
if (mergeNode == mergeNode2){
System.out.println("MergeNode correctly detected");
System.out.println(mergeNode2.mValue);
}
else{
System.out.println("Please check your algorithm!");
}
}
}

The teacher sent this announcement as well:

Please update the code as follows:

int szList1UntilMerge = random.nextInt(100) + 1;
int szList2UntilMerge = random.nextInt(100) + 1;
int szListAfterMerge = random.nextInt(100) + 1;

The goal in this assignment is to exercise Linked List implementation in Java. You are going to implement insert, remove, count and print functionalities of a linkedList. The linked list will be sorted in the ascending order. You are also going to solve the interview question we discussed in class in a very memory efficient way. In this problem, you have 2 linkedLists, and these 2 linkedLists merge at a random node. It may be the 100th node in the first linkedList, and the 4th node in the second linked list. It is completely random as shown below. The goal is to compute this mergeNode in a memory point of view. That is, you could not use extra memory to solve the problem. I added a template project, and commented 6 functions for you to implement. Node class print method, LinkedList class insert, remove, count and print methods and the testDriver class computeTheMergeNode method. Use the main method as it is. Do not update the main method I provided.


We have an Answer from Expert

View Expert Answer

Expert Answer




We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe