Merge Three Singly Linked List Element in Java

Merge three singly linked list element in java. 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

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.

Linked List in Java Program

The LinkedList class, like the ArrayList, is a collection that can hold numerous items of the same type. Because they both implement the List interface, the LinkedList and ArrayList classes have the identical methods. 

The next node in the list is indicated with a pointer. Make a new class called InsertStart with two attributes: head and tail. addAtStart() adds a new node to the list’s beginning: It first determines whether the head is null, indicating that the list is empty. In this program we can execute merge three singly linked list element in java.

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).