Please explain me the problem in this code

import java.util.*;
public class Main
{
public static class Node
{
int data;
Node next;
}
public static class LinkedList
{
private Node head;
private Node tail;
private int size;
public void addLast(int item)
{
Node nn=new Node();
nn.data=item;

		if(size>0)
			tail.next=nn;
		if(size==0)
			head=tail=nn;
		else
			tail=tail.next;
			
		size++;
	}
}

public static void main(String[] args) 
{
	Scanner sc=new Scanner (System.in);
	int t=sc.nextInt();
	while(t!=0)
	{
	    LinkedList list1=new LinkedList();
        LinkedList list2=new LinkedList();
	    int size1=sc.nextInt();
	    for(int i=0;i<size1;i++)
	    {
	        list1.addLast(sc.nextInt());
	    }
	    int size2=sc.nextInt();
	    for(int i=0;i<size2;i++)
	    {
	        list2.addLast(sc.nextInt());
	    }
	    
	    Node l1=list1.head;
	    Node l2=list2.head;
	    
	    while(l1!=null && l2!=null)
	    {
	        if(l1.data<=l2.data)
	        {
	            System.out.print(l1.data+" ");
	            l1=l1.next;
	        }
	        else
	        {
	            System.out.print(l2.data+" ");
	            l2=l2.next;
	        }
	    }
	    
	    if(l1!=null)
	    {
	        while(l1!=null)
	        {
	            System.out.print(l1.data+" ");
	            l1=l1.next;
	        }
	    }
	    else if(l2!=null)
	    {
	        while(l2!=null)
	        {
	            System.out.print(l2.data+" ");
	            l2=l2.next;
	        }
	    }
	    t--;
	}
}

}

@tishachhabra2702_8fc5a68a2e295e35 Very slight change is required. You need to print answer of every iteration in a new line. So add a System.out.println(); at the end of body of the while loop.(Just before t–;)Corrected Code :
import java.util.*;
public class Main
{
public static class Node
{
int data;
Node next;
}
public static class LinkedList
{
private Node head;
private Node tail;
private int size;
public void addLast(int item)
{
Node nn=new Node();
nn.data=item;

		if(size>0)
			tail.next=nn;
		if(size==0)
			head=tail=nn;
		else
			tail=tail.next;
			
		size++;
	}
}

public static void main(String[] args) 
{
	Scanner sc=new Scanner (System.in);
	int t=sc.nextInt();
	while(t!=0)
	{
	    LinkedList list1=new LinkedList();
        LinkedList list2=new LinkedList();
	    int size1=sc.nextInt();
	    for(int i=0;i<size1;i++)
	    {
	        list1.addLast(sc.nextInt());
	    }
	    int size2=sc.nextInt();
	    for(int i=0;i<size2;i++)
	    {
	        list2.addLast(sc.nextInt());
	    }
	    
	    Node l1=list1.head;
	    Node l2=list2.head;
	    
	    while(l1!=null && l2!=null)
	    {
	        if(l1.data<=l2.data)
	        {
	            System.out.print(l1.data+" ");
	            l1=l1.next;
	        }
	        else
	        {
	            System.out.print(l2.data+" ");
	            l2=l2.next;
	        }
	    }
	    
	    if(l1!=null)
	    {
	        while(l1!=null)
	        {
	            System.out.print(l1.data+" ");
	            l1=l1.next;
	        }
	    }
	    else if(l2!=null)
	    {
	        while(l2!=null)
	        {
	            System.out.print(l2.data+" ");
	            l2=l2.next;
	        }
	    }
	    t--;
//added this line
System.out.println();
	}
}

}

1 Like

Thanks, it worked well!!

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.

Grand Visionary Spiritual Master Consultations of the Fa

Return of loving affection, Bring back your lost love, your Husband, or your Wife,
Awaken your Luck, Clairvoyance remote consultation, Wealth, Disenchantment, Client attraction in your shop, workshop, or company, Sexual impotence, Mystical talismans to protect you against shooting or dagger, To have papers abroad, and all kinds of spiritual illness Etc …

Please contact me for more information Contact Whatsapp +229 65418813

Do not hesitate to contact me whatever you want I am available to help you