Write a java program to merge three singly linked list elements

Write a java program to merge three singly linked list elements. A Linked List is a collection of nodes, or objects, that are stored in memory at random. To print as a single output, we can combine or concatenate three singly linked lists. A node has two fields: data stored at that address and a pointer to the memory’s next node. The pointer to null is in the last node of the list.

public class Main
{
Node head;
static class Node
{

int data;
Node next;
Node(int d)
{
data = d;
next=null;

}
}
public void display()
{
Node n = head;
while (n != null)
{
System.out.print(n.data+" \n");
n = n.next;
}
}
public static void main(String[] args)
{
Main Main = new Main();

Main.head = new Node(10);
Node second = new Node(15);
Node third = new Node(20);

Main.head.next = second;
second.next = third;
Main.display();
}
}

 

Output

10

15

20

What is merge sorting in java

One of the most efficient sorting algorithms is merge sort. It is based on the divide-and-conquer strategy. Merge sort continuously cuts down a list into numerous sublists until each sublist contains only one entry, then merges those sublists into a sorted list. Merge sort is a divide-and-conquer algorithm based on the principle of breaking down a list into numerous sub-lists until each sub-list has only one entry, then merging those sub-lists into a sorted list.

What is the efficiency of merge sort in java

Merge Sort is a stable sort, meaning that the same element in an array maintains its original position in relation to other elements in the array. Merge sort has an overall time complexity of O. (nLogn). It is more efficient because the worst-case runtime is also O. (nlogn) Merge sort has an O space complexity (n).