Can i modify given code?

I am not able to understand in given code, how can this code will print after merged two sorted linkedlist?

Hey @Vipin_coder
Question : how can this code will print after merged two sorted linkedlist?

Answer : takes two lists, each of which is sorted in increasing order, and merges the two together into one list which is in increasing order.( simple one list print krna h After merging )
Question : Can I modify given code?
Answer : Nope , just write the logic of merge_sorted_list function

but in given it is passing as list1.merge_sorted_list(list2)

public void merge_sorted_list(LinkedList other) throws Exception {

	// write your code here
	
} 

so, display function is useful, or should i print direct???

Question : should i print direct???
Answer : nope ( convert single sorted List)
Question : display function is useful
Answer : yes

what is mean : list1.merge_sorted_list(list2);
this mean : head is point to list1, and list2 is head of another LinkedList???

if i am allow to write code only inside the merge_sorted_list function, so how given code will print sorted LinkedList, no any display function called in given code.

you can call display function: ( Inside the main)
for now , I am allowing you to print the solution inside the merge_source_list function,

@Vipin_coder
yes head is point to list1
for example :
Node temp1 = this.head; (list1 ke corresponding )
Node temp2 = list2.head;

okay, thank you i will try

i m getting this Error: Could not find or load main class Main

Class name must be main

but i have not changed anything in code as you said, i write only function code

please paste code here

import java.util.*;
class LinkedList {

private class Node {

	int data;
	Node next;
}

private Node head;
private Node tail;
private int size;

public int getFirst() throws Exception {
	if (this.size == 0)
		throw new Exception("linked list is empty");

	return head.data;
}

public int getLast() throws Exception {
	if (this.size == 0)
		throw new Exception("linked list is empty");

	return tail.data;
}

public void addLast(int item) {
	// create a new node
	Node nn = new Node();

	nn.data = item;
	nn.next = null;

	// update summary
	if (size == 0) {
		this.head = nn;
		this.tail = nn;
		size++;
	} else

	{
		this.tail.next = nn;
		this.tail = nn;

		size++;
	}

}

public void addFirst(int item) {
	Node nn = new Node();
	nn.data = item;
	nn.next = null;

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

}

public int removeFirst() throws Exception {
	Node fn = this.head;

	if (this.size == 0)
		throw new Exception("linked list is empty");

	if (this.size == 1) {
		this.head = null;
		this.tail = null;
		size = 0;
	} else {
		Node np1 = this.head.next;
		this.head = np1;
		size--;
	}

	return fn.data;
}

public void merge_sorted_list(LinkedList other) throws Exception {
	Node temp1=this.head;
	Node temp2=other.head;
	Node temptail=null;
	Node temphead=null;
	if(temp1.data<=temp2.data){
		temptail=temp1;
		temphead=temp1;
		temp1=temp1.next;
	}
	else{
		temptail=temp2;
		temphead=temp2;
		temp2=temp2.next;
	}
	while(temp1!=null&&temp2!=null){
		if(temp1.data<=temp2.data){
			temptail.next=temp1;
			temptail=temp1;
			temp1=temp1.next;

		}
		else{
			temptail.next=temp2;
			temptail=temp2;
			temp2=temp2.next;


		}
	}
	while(temp1!=null){
	temptail.next=temp1;
			temptail=temp1;
			temp1=temp1.next;

	}
	
	while(temp2!=null){
	temptail.next=temp2;
			temptail=temp2;
			temp2=temp2.next;

	}
	this.head=temphead;
	display();


	
	
}

public void display() {

	Node temp = this.head;

	while (temp != null) {
		System.out.print(temp.data + " ");
		temp = temp.next;
	}

}

static Scanner scn = new Scanner(System.in);

public static void main(String[] args) throws Exception {
	// TODO Auto-generated method stub
	
	    int t = scn.nextInt();
	    
	    while(t > 0){

		    LinkedList list1 = new LinkedList();
		    int n1 = scn.nextInt();
		 
		    for (int j = 0; j < n1; j++) {
			    int item = scn.nextInt();
			    list1.addLast(item);
		}

            LinkedList list2 = new LinkedList();
		    int n2 = scn.nextInt();
		 
		    for (int j = 0; j < n2; j++) {
			    int item = scn.nextInt();
			    list2.addLast(item);
		}
		   list1.merge_sorted_list(list2);

        t--;
        }
	
}

}

@Vipin_coder
class ka Name Kr diya
import java.util.*;
class Main {

private class Node {

int data;
Node next;

}

private Node head;
private Node tail;
private int size;

public int getFirst() throws Exception {
if (this.size == 0)
throw new Exception(“linked list is empty”);

return head.data;

}

public int getLast() throws Exception {
if (this.size == 0)
throw new Exception(“linked list is empty”);

return tail.data;

}

public void addLast(int item) {
// create a new node
Node nn = new Node();

nn.data = item;
nn.next = null;

// update summary
if (size == 0) {
	this.head = nn;
	this.tail = nn;
	size++;
} else

{
	this.tail.next = nn;
	this.tail = nn;

	size++;
}

}

public void addFirst(int item) {
Node nn = new Node();
nn.data = item;
nn.next = null;

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

}

public int removeFirst() throws Exception {
Node fn = this.head;

if (this.size == 0)
	throw new Exception("linked list is empty");

if (this.size == 1) {
	this.head = null;
	this.tail = null;
	size = 0;
} else {
	Node np1 = this.head.next;
	this.head = np1;
	size--;
}

return fn.data;

}

public void merge_sorted_list(Main other) throws Exception {
Node temp1=this.head;
Node temp2=other.head;
Node temptail=null;
Node temphead=null;
if(temp1.data<=temp2.data){
temptail=temp1;
temphead=temp1;
temp1=temp1.next;
}
else{
temptail=temp2;
temphead=temp2;
temp2=temp2.next;
}
while(temp1!=null&&temp2!=null){
if(temp1.data<=temp2.data){
temptail.next=temp1;
temptail=temp1;
temp1=temp1.next;

	}
	else{
		temptail.next=temp2;
		temptail=temp2;
		temp2=temp2.next;


	}
}
while(temp1!=null){
temptail.next=temp1;
		temptail=temp1;
		temp1=temp1.next;

}

while(temp2!=null){
temptail.next=temp2;
		temptail=temp2;
		temp2=temp2.next;

}
this.head=temphead;
display();

}

public void display() {

Node temp = this.head;

while (temp != null) {
	System.out.print(temp.data + " ");
	temp = temp.next;
}

}

static Scanner scn = new Scanner(System.in);

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

    int t = scn.nextInt();
    
    while(t > 0){

	    Main list1 = new Main();
	    int n1 = scn.nextInt();
	 
	    for (int j = 0; j < n1; j++) {
		    int item = scn.nextInt();
		    list1.addLast(item);
	}

        Main list2 = new Main();
	    int n2 = scn.nextInt();
	 
	    for (int j = 0; j < n2; j++) {
		    int item = scn.nextInt();
		    list2.addLast(item);
	}
	   list1.merge_sorted_list(list2);

System.out.println();
t–;
}

}
}

one more thing, display function for multiple test case will not print in next line for each test case, there is one statement missing.

// I am getting runtime error in test case 2 and 3

 import java.util.*;
class LinkedList {

private class Node {

	int data;
	Node next;
}

private Node head;
private Node tail;
private int size;

public int getFirst() throws Exception {
	if (this.size == 0)
		throw new Exception("linked list is empty");

	return head.data;
}

public int getLast() throws Exception {
	if (this.size == 0)
		throw new Exception("linked list is empty");

	return tail.data;
}

public void addLast(int item) {
	// create a new node
	Node nn = new Node();

	nn.data = item;
	nn.next = null;

	// update summary
	if (size == 0) {
		this.head = nn;
		this.tail = nn;
		size++;
	} else

	{
		this.tail.next = nn;
		this.tail = nn;

		size++;
	}

}

public void addFirst(int item) {
	Node nn = new Node();
	nn.data = item;
	nn.next = null;

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

}

public int removeFirst() throws Exception {
	Node fn = this.head;

	if (this.size == 0)
		throw new Exception("linked list is empty");

	if (this.size == 1) {
		this.head = null;
		this.tail = null;
		size = 0;
	} else {
		Node np1 = this.head.next;
		this.head = np1;
		size--;
	}

	return fn.data;
}

public void merge_sorted_list(LinkedList other) throws Exception {
	Node temp1=this.head;
	Node temp2=other.head;
	Node temptail=null;
	Node temphead=null;
	if(temp2==null){
	this.display();
	return;
	}
 	if(temp1==null){
		 this.head=temp2;
		 this.display();
	 }
	if(temp1.data<=temp2.data){
		temptail=temp1;
		temphead=temp1;
		temp1=temp1.next;
	}
	else{
		temptail=temp2;
		temphead=temp2;
		temp2=temp2.next;
	}
	while(temp1!=null&&temp2!=null){
		if(temp1.data<=temp2.data){
			temptail.next=temp1;
			temptail=temp1;
			temp1=temp1.next;

		}
		else{
			temptail.next=temp2;
			temptail=temp2;
			temp2=temp2.next;


		}
	}
	while(temp1!=null){
	temptail.next=temp1;
			temptail=temp1;
			temp1=temp1.next;

	}
	
	while(temp2!=null){
	temptail.next=temp2;
			temptail=temp2;
			temp2=temp2.next;

	}
	this.head=temphead;
	display();


	
	
}

public void display() {

	Node temp = this.head;

	while (temp != null) {
		System.out.print(temp.data + " ");
		temp = temp.next;
	}
	System.out.println();

}

}

public class Main{
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();

	    while(t > 0){

		    LinkedList list1 = new LinkedList();
		    int n1 = scn.nextInt();
		 
		    for (int j = 0; j < n1; j++) {
			    int item = scn.nextInt();
			    list1.addLast(item);
		}

            LinkedList list2 = new LinkedList();
		    int n2 = scn.nextInt();
		 
		    for (int j = 0; j < n2; j++) {
			    int item = scn.nextInt();
			    list2.addLast(item);
		}
		   list1.merge_sorted_list(list2);

        t--;
        }
	
}

}

try for this input :
1
0
3
1 2 3
Getting NullPointerException