so the question is to print out strings that appear both in the two given lists, assume each list does not contain duplicates of the same word.
so with the signiture of
public void intersect(Node n1,Node n2)
and suppose we already have another method:
public boolean contains(Node n, String target)
this is to give true if n contains a string target,false otherwise.
my solution is
public void intersect(Node n1,Node n2){
Node current2=n2;
while(current2 !=null){
if(contains(n1,current2.val)==true)
System.out.println(current2.val);
current2=curren2.next;
else
current2=current2.next;
}//so basically this is to use contains to find if the first string data in n2 is also in n1.if so,print this data in n2 and go to next node in n2 and do it again..
}
so is this algo looks fine?
another question is now the two lists are both sorted,and you have to write another method that prints out the same data in both lists,with O(n).
so what difference this this make? can I still use my first solution if it works? coz the first solution looks with O(n)too.wait ,is it?