Code issue , , , , , ,, , , , , , , , , , ,

can you check my code why it didnt pass test cases


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;
}
}
int count1=0;
public void insert1(int n)
{
count1++;
Node node = new Node(n);
if(head1==null)
{
head1=node;
}
else
{
Node t=head1;
while(t.next!=null)
{
t=t.next;
}
t.next=node;
}
}

public void merge()
{
    Node t=head;
Node n=head1;
Node f=null;

        
         while(t!=null&&n!=null)
         {
             if(t.data>=n.data)
             {
                 f=add(f,n.data);
                 n=n.next;
             }
             else
             {
                 f=add(f,t.data);
                 t=t.next;
             }
         }
    if(n==null)
    {
        if(t!=null)
        {
            while(t!=null)
            {
                f= add(f,t.data);
                 t=t.next;
            }
        }
    }
    if(t==null)
    {
        if(n!=null)
        {
             while(n!=null)
            {

f=add(f,n.data);
n=n.next;
}
}
}
while(f!=null)
{
System.out.print(f.data+" ");
f=f.next;
}
}
public Node add(Node f,int d)
{
Node l = new Node(d);
if(f==null)
{
f=l;
}
else
{
Node temp=f;
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=l;
}
return f;
}
}
public class Main {
public static void main(String args[]) {
Linkedlist list = new Linkedlist();
Scanner obj = new Scanner(System.in);
int t=obj.nextInt();
while(t>0)
{
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.merge();
t–;
}
}
}

@Nitin-Mishra-2380486738834604,

The Approach is pretty simple that:

we will Start from the head of the two lists and consume that element from the two of the Lists first which is smaller and add that element into the ans list.

After consuming, Advance the iterator to point to the next node in the list. The size of the Lists can be different so after consuming elements simultaneously from the two list, We need to check if any of the two list is empty, and add the elements of that list as it is in the answer List.

You can implement this approach and reply on this thread if you need any help.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.