VOOZH about

URL: https://www.geeksforgeeks.org/dsa/merge-sort-for-doubly-linked-list/

⇱ Merge Sort for Doubly Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Merge Sort for Doubly Linked List

Last Updated : 23 Jul, 2025

Given a doubly linked list, The task is to sort the doubly linked list in non-decreasing order using merge sort.

Examples:

Input: 10 <-> 8 <-> 4 <-> 2
Output: 2 <-> 4 <-> 8 <-> 10

Input: 5 <-> 3 <-> 2
Output: 2 <-> 3 <-> 5

Note:Merge sort for a singly linked list has already been discussed. The important change here is to modify the previous pointers when merging two lists.

Approach :

The idea is to maintain a MergeSort function that sorts the list in three steps:

Divide: Split the list into two halves using a mid node. The first half runs from the head to just before mid, and the second half starts at mid and runs to the end.
Recursively Sort: Apply MergeSort recursively on both halves.
Merge: Merge the two sorted halves into one sorted list and return the new head node.

The MergeSort function will return the node representing the new head of the sorted doubly linked list.

Below is the implementation of above approach :


Output
2 5 8 10 

Time Complexity: O(nLogn)  
Auxiliary Space: O(1)

Related Articles:

Comment