Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Intersection {
- public static void intersection(SLL<Integer> list1, SLL<Integer> list2){
- SLLNode<Integer> tmp1=list1.getFirst();
- SLLNode<Integer> tmp2;
- boolean done=false;
- while (tmp1!=null){
- tmp2=list2.getFirst();
- while (tmp2!=null){
- if(tmp1.element==tmp2.element){
- System.out.println(tmp1.element+" ");
- tmp1=tmp1.succ;
- tmp2=tmp2.succ;
- done=true;
- }
- else{
- tmp2=tmp2.succ;
- }
- }
- if(tmp2==null && !done)
- tmp1=tmp1.succ;
- if(done)
- break;
- }
- if(!done){
- System.out.println("No intersection");
- }
- }
- public static void main(String[] args) {
- Scanner in=new Scanner(System.in);
- int n1=in.nextInt();
- SLL<Integer> list1=new SLL<>();
- for(int i=0;i<n1;i++){
- list1.insertLast(in.nextInt());
- }
- int n2=in.nextInt();
- SLL<Integer> list2=new SLL<>();
- for(int i=0;i<n2;i++){
- list2.insertLast(in.nextInt());
- }
- intersection(list1,list2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement