Prepin
Log in

Sort a Doubly Linked List with Merge Sort.

Approach / Explanation

The inquiry 'Sort a Doubly Linked List using Merge Sort' requires you to implement the merge sort algorithm for a doubly linked list. A doubly linked list is a linear data structure where each element points to both the next and previous elements, providing enhanced access and data modification capabilities. Merge sort is a sorting algorithm that operates using a divide and conquer strategy. It involves splitting the list into two halves, sorting each half, and then merging them back together to create a sorted list. Here are some steps to assist you in addressing the question: Familiarize yourself with the structure of a doubly linked list. Grasp the workings of the merge sort algorithm. Develop a function to divide a doubly linked list into two equal parts. Create a function to merge two sorted lists into one. Implement a function to perform merge sort on the doubly linked list.

Suggested Answer

Initially, we need a function to divide the linked list into two equal segments. Additionally, we require a function to combine the sorted segments. Below is the implementation: public Node mergeSort ( Node node ) { if ( node == null || node . next == null ) { return node ; } Node second = split ( node ) ; // Recursively sort the left and right halves node = mergeSort ( node ) ; second = mergeSort ( second ) ; // Combine the two sorted halves return merge ( node , second ) ; } In the Java function above, the 'split' and 'merge' functions can be defined as follows: // Divide a doubly linked list (DLL) into two DLLs of equal size Node split ( Node head ) { Node fast = head , slow = head ; 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 merge ( Node first , Node second ) { // If the first linked list is empty if ( first == null ) { return second ; } // If the second linked list is empty if ( second == null ) { return first ; } // Choose 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 ; } }

Alternative Answer

To organize a doubly linked list using merge sort, the first step is to split the list into two halves. Next, each half is sorted, and finally, they are combined into one sorted list. Below is the Python implementation of this process: def mergeSort ( self , tempHead ) : if tempHead is None : return tempHead if tempHead . next is None : return tempHead second = self . split ( tempHead ) # Recursively sort the left and right halves tempHead = self . mergeSort ( tempHead ) second = self . mergeSort ( second ) # Merge the two sorted halves return self . merge ( tempHead , second ) The 'split' and 'merge' functions can also be implemented in Python as follows: def split ( self , tempHead ) : fast = slow = tempHead while ( True ) : if fast . next is None : break if fast . next . next is None : break fast = fast . next . next slow = slow . next temp = slow . next slow . next = None return temp def merge ( self , first , second ) : # If the first linked list is empty if first is None : return second # If the second linked list is empty if second is None : return first # Choose the smaller value and # call recursively for the subsequent elements if first . data < second . data : first . next = self . merge ( first . next , second ) first . next . prev = first first . prev = None return first else : second . next = self . merge ( first , second . next ) second . next . prev = second second . prev = None return second This code in Python also segments the list into two parts, sorts them individually, and then merges them back into one sorted list.

Question Details

Difficulty & Category

Medium
data structures

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.