Merge sorted linked lists

package linkedlists;
import java.util.*;
class LinkedList {

private class Node {

	int data;
	Node next;
	
	public void Node() {
		this.data = data;
		this.next= next;
	}
}

private static final Node Node = null;

private static 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 static Node mergeSorted(Node l1,Node l2) {
		if(l1 == null) return l2;
		if(l2 == null) return l1;
		
		Node l3 = null;
		if(l1.data<l2.data) {
			l3 = l1;
			l1 = l1.next;
		}else {
			l3 = l2;
			l2 = l2.next;
		}
		
		Node currentnode = l3;
		while(l1!=null && l2!=null) {
			if(l1.data < l2.data) {
				currentnode.next = l1;
				l1 = l1.next;
			}
			else {
				currentnode.next = l2;
				l2 = l2.next;
			}
			currentnode =currentnode.next;
		}
		
		if(l1 == null) {
				currentnode.next = l2;
		}else {
			currentnode.next = l1;
		}
		
		return l3;
	
	}

	

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();
		    Node l1 =list1.head;
		    int n1 = scn.nextInt();
		 
		    for (int j = 0; j < n1; j++) {
			    int item = scn.nextInt();
			    list1.addLast(item);
		}

            LinkedList list2 = new LinkedList();
            Node l2 = list2.head;
		    int n2 = scn.nextInt();
		 
		    for (int j = 0; j < n2; j++) {
			    int item = scn.nextInt();
			    list2.addLast(item);
		}
		   Node ans = mergeSorted(l1,l2);
		   while(ans != null) {
			   System.out.print(ans.data+" ");
			   ans = ans.next;
		   }
        t--;
        }
	
}

}
//sir I’m getting the a different result as 1 3 5 7 but I’m not able to find any flaw in my logic pls help

@Siddharth_sharma1808,
https://ide.codingblocks.com/s/218622 Here is the corrected code. Instead of returning the head node, return a new linked list which is your answer.

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.

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.