# Merge Two Unsorted Linked Lists To Get A Sorted List

### Introduction

One of the most crucial data structures to learn while preparing for interviews is the linked list. In a coding interview, having a thorough understanding of Linked Lists might be a major benefit.

### Problem Statement

In this problem, we are given two unsorted linked lists, and we have to merge these two linked lists to get a sorted linked list.

Prerequisites: Bubble sort on linked list.

### Problem Statement Understanding

Let’s try to understand the problem statement with the help of examples.

Let’s say the given linked lists are :
List1 = head → 1 → 6 → 10 → NULL , List2 = head → 5 → 3 → 9 → NULL • So now, according to the problem statement, the given linked lists List1 and List2 are unsorted, and we need to merge them to get a sorted linked list.
• After merging the linked lists, we need to return the head of the merged linked list.
• After merging the two linked lists in the above example, the sorted linked lists will be head → 1 → 3 → 5 → 6 → 9 → 10 → NULL. The linked list will be sorted in ascending order.

#### Some more Examples

Example 1:
Input:
List1 = head → 4 → 50→ 12 → NULL
List2 = head → 10 →1→ 60 → NULL

Output :
head → 1 → 4 → 10→12 → 50 → 60

Example 2:
Input:
List 1 = head → 10 → 20 → 60 → NULL
List 2 = head → 10 → 50 → 30 → NULL

Output:
head →10 → 10 → 20 → 30 → 50 → 60 → NULL

At this point, we have understood the problem statement. Now we will try to formulate an approach for this problem.

Before moving to the approach section, try to think about how you can approach this problem.

• If stuck, no problem, we will thoroughly see how we can approach this problem in the next section.

Let’s move to the approach section.

### Naive Approach

The naive approach to solve this problem is to first sort the two linked lists and then merge these sorted linked lists into one linked list in increasing order.

Time Complexity
O(N2 + M2 + (N+M)), where M and N are the lengths of the linked lists. We are first sorting both the lists. Sorting a list takes quadratic time (using bubble sort for sorting). Then we will be merging the two sorted list which will take O(N+M) time. Hence, the total time complexity will be O(N2 + M2 + (N+M)).

Space Complexity:
0(1), We are using only a constant amount of extra space.

### Efficient Approach

In the efficient approach, we will first concatenate the two given linked lists and then sort the final concatenated linked list by using a sorting algorithm.

Here we will be using bubble sort to sort the linked list. To see how bubble sort works on a linked list, check out this article.

### Algorithm

The algorithm of this problem is below:
1) First, we will concatenate the two lists.

• In the first list, we will traverse the list until we reach its tail, and after reaching the tail, we will point the next of the tail node to the head node of the second list, storing the concatenated in the first list.
2) Then, we will sort this merged list (we will use bubble sort).
• While sorting, if node→next→data is less than node→data, then we will swap the data.

### Dry Run  ### Code Implementation

```public class Prepbytes {

static class node {
int data;
node next;
};

node tmp;

while (tmp.next != null) {
System.out.print(tmp.data + " -> ");
tmp = tmp.next;
}
System.out.println(tmp.data );
}

// Creating a new node
node temp = new node();

temp.data = num;
temp.next = null;

tail = temp;
}

// Else insert the temp node
// after the tail of the existing
// node and make the temp node
// as the tail of the linked list
else {
while (tail != null) {
if (tail.next == null) {
tail.next = temp;
tail = tail.next;
}
tail = tail.next;
}
}

// Return the list
}

// This function will concatenate
static node concatenateList() {

while (tail != null) {
if (tail.next == null && head2 != null) {
break;
}
tail = tail.next;
}

}

// This function will sort the linked list
static void sort() {

// Compares the elements
// If node->next->data is less than node->data
// then we will swap the data.
while (current.next != null) {
temp = current.next;
while (temp != null) {
if (temp.data < current.data) {
int t = temp.data;
temp.data = current.data;
current.data = t;
}
temp = temp.next;
}
current = current.next;
}
}

public static void main(String[] args) {
System.out.println("List1:");
System.out.println("List2:");

sort();
System.out.println("Final merged list:");
}
}
```

#### Output

List1:
1 -> 6 -> 10
List2:
5 -> 3 -> 9
Final merged list:
1 -> 3 -> 5 -> 6 -> 9 -> 10

Time Complexity
O((N+M)2), where M and N are the lengths of the linked lists. We are merging given linked lists and iterating the merged list using nested loops to perform bubble sort. The time complexity of bubble sort is O((list.length)2). Hence, the time complexity will be O(((N+M)2).

Space Complexity

Note: Also note that to reduce the time complexity, we can use merge sort instead of bubble sort. Merge sort will have a time complexity of O(N * logN), whereas bubble sort has complexity of O(N2).

So, in this blog, we have tried to explain how you can merge two unsorted linked lists to get a sorted list. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.