Merge K sorted Doubly Linked List in Sorted Order | javascript program to merge K sorted doubly linked list in sorted order A linked list node ; 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
class Node { constructor ( ) { this . data = 0 ; this . next = null ; this . prev = null ; } } class Node { constructor ( ) { this . data = 0 ; this . next = null ; this . prev = null ; } } function append ( head_ref , new_data ) { new_node = new 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 head_ref ; } while ( last . next != null ) last = last . next ; last . next = new_node ; new_node . prev = last ; return head_ref ; } function printList ( node ) { last ; while ( node != null ) { document . write ( node . data + " " ) ; last = node ; node = node . next ; } } function mergeList ( p , q ) { 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 ; } 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 ; } function mergeAllList ( head , k ) { finalList = null ; for ( i = 0 ; i < k ; i ++ ) { finalList = mergeList ( finalList , head [ i ] ) ; } return finalList ; } var k = 3 ; head = Array ( k ) . fill ( null ) ; for ( i = 0 ; i < k ; i ++ ) { head [ i ] = null ; } head [ 0 ] = append ( head [ 0 ] , 1 ) ; head [ 0 ] = append ( head [ 0 ] , 5 ) ; head [ 0 ] = append ( head [ 0 ] , 9 ) ; head [ 1 ] = append ( head [ 1 ] , 2 ) ; head [ 1 ] = append ( head [ 1 ] , 3 ) ; head [ 1 ] = append ( head [ 1 ] , 7 ) ; head [ 1 ] = append ( head [ 1 ] , 12 ) ; head [ 2 ] = append ( head [ 2 ] , 8 ) ; head [ 2 ] = append ( head [ 2 ] , 11 ) ; head [ 2 ] = append ( head [ 2 ] , 13 ) ; head [ 2 ] = append ( head [ 2 ] , 18 ) ; finalList = mergeAllList ( head , k ) ; printList ( finalList ) ;