Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Select three methods in the object List class to work through algorithmically. D

ID: 3930982 • Letter: S

Question

Select three methods in the object List class to work through algorithmically. Describe any special cases o0r boundary conditions that might exist for each of the three methods selected. Be sure to draw pictures as you work through the methods. Remember that you cannot work on linked lists without drawing pictures. The homework will not be accepted without submitting the picture! Write a Java method with the signature: that accepts two unordered linear linked lists and returns a third linear linked list whose nodes contains the, intersection of the two original linear linked lists.

Explanation / Answer

Answer:

import java.util.*;

public class Solution {
  
public static void main(String[] args) {
  
LinkedList<Integer> list1, list2;
  
list1 = new LinkedList<Integer>();
  
list2 = new LinkedList<Integer>();
  
for(int i=0; i<10; i++) {

list1.add((int)(10*Math.random()));
list2.add((int)(10*Math.random()));
}
  
System.out.println("List 1: "+list1);
System.out.println("List 2: "+list2);
  
System.out.println(" Intersection: "+intersect(list1, list2));
  
}
  
public static LinkedList<Integer> intersect (LinkedList<Integer> list1, LinkedList<Integer> list2) {

Iterator<Integer> i = list1.iterator();
TreeSet<Integer> common = new TreeSet<Integer>();
  
while(i.hasNext()) {
  
int val1 = i.next();
Iterator<Integer> j = list2.iterator();
  
while(j.hasNext()) {
  
int val2 = j.next();
  
if(val1 == val2) {
common.add(val1);
break;
}
}
}
  
LinkedList<Integer> intersectionList = new LinkedList<Integer>();
  
intersectionList.addAll(common);
  
return intersectionList;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote