Courses βeta
Data Structures

Given a doubly linked list, following is a function to sort the doubly linked list in increasing order using merge sort.

For example, the following doubly linked list should be changed to 2<->4<->8<->10

Merge sort for singly linked list is already discussed. The important change here is to modify the previous pointers also when merging two lists.

Below is the implementation of merge sort for doubly linked list.

// C program for merge sort on doubly linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next, *prev;
};

// Function to merge two linked lists
struct node *merge(struct node *first, struct node *second)
{
// If first linked list is empty
if (!first)
return second;

// If second linked list is empty
if (!second)
return first;

// Pick the smaller value
if (first->data < second->data)
{
first->next = merge(first->next,second);
first->next->prev = first;
first->prev = NULL;
return first;
}
else
{
second->next = merge(first,second->next);
second->next->prev = second;
second->prev = NULL;
return second;
}
}

// Function to do merge sort
{

// Recur for left and right halves
second = mergeSort(second);

// Merge the two sorted halves
}

// A utility function to insert a new node at the
// beginning of doubly linked list
void insert(struct node **head, int data)
{
struct node *temp =
(struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->next = temp->prev = NULL;
else
{
}
}

// A utility function to print a doubly linked list in
// both forward and backward directions
{
printf("Forward Traversal using next poitner\n");
{
}
printf("\nBackword Traversal using prev pointer\n");
while (temp)
{
printf("%d ", temp->data);
temp = temp->prev;
}
}

// Utility function to swap two integers
void swap(int *A, int *B)
{
int temp = *A;
*A = *B;
*B = temp;
}

// Split a doubly linked list (DLL) into 2 DLLs of
// half sizes
{
while (fast->next && fast->next->next)
{
fast = fast->next->next;
slow = slow->next;
}
struct node *temp = slow->next;
slow->next = NULL;
return temp;
}

// Driver program
int main(void)
{
return 0;
}


// Java program to implement merge sort in singly linked list

/* Node Class */
static class Node {

int data;
Node next, prev;

// Constructor to create a new node
Node(int d) {
data = d;
next = prev = null;
}
}

void print(Node node) {
Node temp = node;
System.out.println("Forward Traversal using next pointer");
while (node != null) {
System.out.print(node.data + " ");
temp = node;
node = node.next;
}
System.out.println("\nBackward Traversal using prev pointer");
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.prev;
}
}

// Split a doubly linked list (DLL) into 2 DLLs of
// half sizes
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
Node temp = slow.next;
slow.next = null;
return temp;
}

Node mergeSort(Node node) {
if (node == null || node.next == null) {
return node;
}
Node second = split(node);

// Recur for left and right halves
node = mergeSort(node);
second = mergeSort(second);

// Merge the two sorted halves
return merge(node, second);
}

// Function to merge two linked lists
Node merge(Node first, Node second) {
// If first linked list is empty
if (first == null) {
return second;
}

// If second linked list is empty
if (second == null) {
return first;
}

// Pick the smaller value
if (first.data < second.data) {
first.next = merge(first.next, second);
first.next.prev = first;
first.prev = null;
return first;
} else {
second.next = merge(first, second.next);
second.next.prev = second;
second.prev = null;
return second;
}
}

// Driver program to test above functions
public static void main(String[] args) {

Node node = null;
list.print(node);

}
}


Output:

Linked List after sorting
Forward Traversal using next pointer
3 4 5 10 20 30
Backward Traversal using prev pointer
30 20 10 5 4 3

Time Complexity:
Time complexity of the above implementation is same as time complexity of MergeSort for arrays. It takes (nLogn) time.

You may also like to see QuickSort for doubly linked list