my code not passing 1 test case can you check ?
import java.util.*;
class Node
{
int data;
Node next;
public Node(int n)
{
data=n;
}
}
class Linkedlist
{
Node head;
Node head1;
int count=0;
public void insert(int n)
{
count++;
Node node = new Node(n);
if(head==null)
{
head=node;
}
else
{
Node t=head;
while(t.next!=null)
{
t=t.next;
}
t.next=node;
}
}
public void insert1(int n)
{
Node node = new Node(n);
if(head1==null)
{
head1=node;
}
else
{
Node t=head1;
while(t.next!=null)
{
t=t.next;
}
t.next=node;
}
}
boolean flag=false;
int a=0;
Node s=head1;
public void check()
{
while(head!=null)
{
while(s!=null)
{
if(head.data==s.data)
{
flag=true;
a=head.data;
break;
}
s=s.next;
}
s=head1;
if(flag)
{
System.out.println(a);
break;
}
head=head.next;
}
if(flag==false)
{
System.out.println("-1");
}
}
}
public class Main {
public static void main(String args[]) {
Linkedlist list = new Linkedlist();
Scanner obj = new Scanner(System.in);
int n=obj.nextInt();
for(int i=0; i<n; i++)
{
int m=obj.nextInt();
list.insert(m);
}
int k=obj.nextInt();
for(int i=0; i<k; i++)
{
int m=obj.nextInt();
list.insert1(m);
}
list.check();
}
}