text: Merge K sorted Doubly Linked List in Sorted Order | C ++ program to merge K sorted doubly linked list in sorted order ; A linked list node ; Given a reference ( pointer to pointer ) to the head Of a DLL and an int , appends a new node at the end ; Allocate node ; Put in the data ; This new node is going to be the last node , so make next of it as NULL ; If the Linked List is empty , then make the new node as head ; Else traverse till the last node ; Change the next of last node ; Make last node as previous of new node ; Function to print the list ; Run while loop unless node becomes null ; Function to merge two sorted doubly linked lists ; If any of the list is empty ; Comparison the data of two linked list ; Store head pointer before merge the list ; Changing of pointer between Two list for merging ; Changing of pointer between Two list for merging ; Condition to check if any anyone list not end ; Return head pointer of merged list ; Function to merge all sorted linked list in sorted order ; Function call to merge two sorted doubly linked list at a time ; Return final sorted doubly linked list ; Driver code ; Loop to initialize all the lists to empty ; Create first doubly linked List List1 -> 1 <= > 5 <= > 9 ; Create second doubly linked List List2 -> 2 <= > 3 <= > 7 <= > 12 ; Create third doubly linked List List3 -> 8 <= > 11 <= > 13 <= > 18 ; Function call to merge all sorted doubly linked lists in sorted order ; Print final sorted list
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; Node * prev ; } ; void append ( struct Node * * head_ref , int new_data ) { struct Node * new_node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; struct Node * last = * head_ref ; new_node -> data = new_data ; new_node -> next = NULL ; if ( * head_ref == NULL ) { new_node -> prev = NULL ; * head_ref = new_node ; return ; } while ( last -> next != NULL ) last = last -> next ; last -> next = new_node ; new_node -> prev = last ; return ; } void printList ( Node * node ) { Node * last ; while ( node != NULL ) { cout << node -> data << " ▁ " ; last = node ; node = node -> next ; } } Node * mergeList ( Node * p , Node * q ) { Node * s = NULL ; if ( p == NULL q == NULL ) { return ( p == NULL ? q : p ) ; } if ( p -> data < q -> data ) { p -> prev = s ; s = p ; p = p -> next ; } else { q -> prev = s ; s = q ; q = q -> next ; } Node * head = s ; while ( p != NULL && q != NULL ) { if ( p -> data < q -> data ) { s -> next = p ; p -> prev = s ; s = s -> next ; p = p -> next ; } else { s -> next = q ; q -> prev = s ; s = s -> next ; q = q -> next ; } } if ( p == NULL ) { s -> next = q ; q -> prev = s ; } if ( q == NULL ) { s -> next = p ; p -> prev = s ; } return head ; } Node * mergeAllList ( Node * head [ ] , int k ) { Node * finalList = NULL ; for ( int i = 0 ; i < k ; i ++ ) { finalList = mergeList ( finalList , head [ i ] ) ; } return finalList ; } int main ( ) { int k = 3 ; Node * head [ k ] ; for ( int i = 0 ; i < k ; i ++ ) { head [ i ] = NULL ; } append ( & head [ 0 ] , 1 ) ; append ( & head [ 0 ] , 5 ) ; append ( & head [ 0 ] , 9 ) ; append ( & head [ 1 ] , 2 ) ; append ( & head [ 1 ] , 3 ) ; append ( & head [ 1 ] , 7 ) ; append ( & head [ 1 ] , 12 ) ; append ( & head [ 2 ] , 8 ) ; append ( & head [ 2 ] , 11 ) ; append ( & head [ 2 ] , 13 ) ; append ( & head [ 2 ] , 18 ) ; Node * finalList = mergeAllList ( head , k ) ; printList ( finalList ) ; return 0 ; }