Answer to Question #309551 in Java | JSP | JSF for Kyle

Question #309551

Write a program using Java to find the number of common elements of two linked lists.

e.g. 1 3 5 7 null

        2 3 7 9 11 null

Output: 2


1
Expert's answer
2022-03-18T08:54:32-0400
import java.util.Arrays;
import java.util.LinkedList;

public class solution {

	public static void main(String[] args) {
		LinkedList<Integer> l1 = new LinkedList<Integer>(Arrays.asList(1, 3, 5, 7, null));
		LinkedList<Integer> l2 = new LinkedList<Integer>(Arrays.asList(2, 3, 7, 9, 11, null));
		
		int counter = 0;

		for (int i = 0; i < l1.size() - 1; i++) {
			if (l2.contains(l1.get(i))) {
				counter ++;
			}
		}
		
		System.out.println(counter);
	}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS