sorting a doubly linked list with merge sort
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Merge sort is one of the best sorting algorithms for linked lists because it does not require random access. On a doubly linked list, it is especially convenient because nodes can be split and merged by rewiring pointers instead of copying array elements.
Why Merge Sort Fits Linked Lists
Array-based sorts often depend on indexing into the middle of the structure or moving many elements around. Linked lists are not good at either of those things. Merge sort, by contrast, only needs:
- a way to split the list into halves
- a way to merge two already-sorted lists
Both operations are natural on linked lists.
The overall complexity remains O(n log n), and merge sort is stable when implemented carefully. That means equal elements keep their original relative order, which can matter when nodes store records with secondary meaning.
Core Steps
Sorting a doubly linked list with merge sort follows the same high-level recipe as array merge sort:
- find the middle of the list
- split the list into two halves
- recursively sort each half
- merge the two sorted halves
The main difference is pointer maintenance. Because this is a doubly linked list, both next and prev pointers must stay consistent after every merge.
C++ Implementation
The following example sorts a doubly linked list of integers.
This code shows the essential pieces: split, recursive sort, and merge.
The Split Step Matters
The slow-and-fast pointer technique is the standard way to find the midpoint of a linked list. Once the middle is found, the list is cut into two independent sublists.
The important detail in a doubly linked list is not just slow->next = nullptr, but also resetting the second half's prev pointer to nullptr. If you forget that step, the list may still sort partially, but backward traversal will be broken.
The Merge Step Must Repair Both Directions
When merging, many implementations remember to set next and forget to repair prev. That leads to a list that looks fine when printed forward but fails when traversed backward or when later operations rely on prev links.
A correct merge does three things repeatedly:
- chooses the smaller head node
- links its
nextpointer to the merged remainder - repairs the chosen remainder's
prevpointer back to the current node
That is the doubly linked list version of merge sort in one sentence.
Common Pitfalls
- Splitting the list without clearing the second half's
prevpointer. - Merging correctly in the forward direction but leaving backward links broken.
- Forgetting the base case for empty and one-node lists.
- Accidentally creating cycles by reusing old pointers after splitting.
- Using data swapping instead of pointer rewiring when the real goal is to sort nodes structurally.
Summary
- Merge sort is a strong fit for doubly linked lists because it works through pointer manipulation rather than indexing.
- The algorithm runs in
O(n log n)time and is stable when implemented carefully. - Use slow and fast pointers to split the list into halves.
- During merging, maintain both
nextandprevlinks correctly. - Always test forward traversal and backward traversal after sorting.

