Merge Sorted LinkedList

I don’t Know why I am getting runtime errors


import java.util.*;
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);
		   list1.display();
		   System.out.println();

        t--;
        }
	
}

}
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 head1 = head;
	Node head2 = other.head;

	if(head1 == null)
	{ 
		head = head2;
		return;
	}

	else if(head2 == null)
	{
		return;
	}

	Node head3 = null;

	if(head1.data < head2.data)
	{
		head3 = head1;
		head1 = head1.next;
	}
	else
	{
		head3 = head2;
		head2 = head2.next;
	}
	

	Node current = head3;

	while(head1 != null && head2 != null)
	{
		if(head1.data<head2.data)
		{
			current.next = head1;
			head1 = head1.next;
		}
		else if(head2.data < head1.data){
			current.next = head2;
			head2 = head2.next;
		}
		// else
		// {
		// 	current.next = head2;
		// 	head2 = head2.next;
		// 	head1 = head1.next;
		// }
		current = current.next;
	}

	if(head1 == null)
		current.next = head2;
	else
		current.next = head1;

	head = head3;
}

public void display() {

	Node temp = this.head;

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

}

}

@arjunsabu99 bro check ya code for this test case.

1
2
1 1
2
1 1

If its clear mark the doubt resolved

output is coming out to be 1 1 1 1

@arjunsabu99 the code you submitted here is giving null pointer exception if you have made changes then submit your modified code bro!