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?

Recommended Answers

All 4 Replies

anyone help please...:)

if(contains(n1,current2.val)==true)

is the same as

if(contains(n1,current2.val))

Are you not writing the contains(A,B) method? this will influence the number of computations, if it is a sorted list, you wont have to go through each element, there are faster ways

thanks hanvyi,
yes,i have written the contains method,but the efficiency is not one of my concern right now. i just wannted to check if my intersect method make sense.
anyway, if the two lists are sorted,how will it be faster.am i not supposed to compare each element too?coz each list now will be sorted,i.e.alphabetic order since it is a string.so we have like,list1: animal,bus,school list2: animal,butt,life.
we still need to compare if the two words are the same,is it not?

you can binary search

http://en.wikipedia.org/wiki/Binary_search_algorithm

check whether your word is "greater" or "less than" than half way, then "cut" the list in half, test the half its in with the same criteria, much faster than going through each element asking whether it is equal!

As to your funcion, use tabs

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..
	}

it makes thing much clearer, for example you can see your if statement is a bit messed up, I would advise to allways use { and } for if statments, expecially if you use else

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..
	}

is what i belive you meant to put, although the else statment is pointless,

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=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..
	}

is equivilent.

(sorry for my spelling) + just noticed "curren2" in the curren2.next() of the if statment should have a t

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.