Need help on problem 3 Select three methods in the objectLis
Need help on problem #3
Select three methods in the objectList class to work through algorithmically. Describe any special cases or 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 pictures! Write a Java method with the signature. Public ObjectList intersect (objectList list1, objectList list2) that accepts two unordered linear linked lists and returns a third linear linked whose nodes contains the intersection of the two original linear linked lists. Note that the intersection of two lists is a new list containing the elements that the two list have in common, without modifying the two original lists Describe any boundary conditions that might exist.Solution
public ObjectList intersect(ObjectList list1, ObjectList list 2){
// we will use two nested loop to find out point of intersection
ObjectList ans = new ObjectList(); // dummy node, next of ans node will be answer
int l1 = length(list1);
int l2 = length(list2);
ObjectList node1 = list1;
ObjectList node2 = list2;
ObjectList ans_pointer = ans;
for (int i = 0; i < l1 ; i ++ ) {
// iterate through first list
for (int j = 0 ;j < l2 ; j ++ ) {
// iterate throuh 2nd list
if(node1 == node2){
// if match found
while(node1 == node2){
// while intersection continues
ans_pointer.next = node1; // keep adding new nodes
ans_pointer = ans_pointer.next;
node1 = node1.next;
node2 = node2.next;
}
return ans.next;
}
node2 = node2.next;
}
node1 = node1.next;
}
}
