Merge sorted Linked list question getting run error

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 {

	// write your code here
	
	LinkedList merge = new LinkedList();

	

	
	int i=0,j=0,k=0;

	if(this.head.data<=other.head.data)
	{
		merge.addLast(this.head.data);
		
		this.head=this.head.next;
		
		
		i++;
		k++;
	}
	else{
		merge.addLast(other.head.data);
		other.head=other.head.next;
		j++;
		k++;
	}
	while(i<this.size && j<other.size)
	{
		if(this.head.data<=other.head.data)
		{
			merge.addLast(this.head.data);
			this.head=this.head.next;
			i++;
			k++;
		}
		else{
			merge.addLast(other.head.data);
			
			other.head=other.head.next;
			j++;
			k++;
			}	
		}
	if(i==this.size)
	{
		while(j<other.size)
		{
			merge.addLast(other.head.data);
			other.head=other.head.next;
			j++;
			k++;
		}
	}
	if(j==other.size)
	{
		while(i<this.size)
		{
			merge.addLast(this.head.data);
			this.head=this.head.next;
			i++;
			k++;
		}
	}

	merge.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--;
        }
	
}

}

You are changing head pointer of the linklist. Dont do that. Take a temp pointer and vary that pointer.

Done the changes,then also not working.

It was working after I made temp pointer to vary throughout the list . Share your code if you are still struck at the code.