Sort the linked list in the order of elements appearing in the array | Linked list node ; Function to insert a node at the beginning of the linked list ; function to print the linked list ; Function that sort list in order of apperaing elements in an array ; Store frequencies of elements in a hash table . ; One by one put elements in lis according to their appearance in array ; Update ' frequency ' nodes with value equal to arr [ i ] ; Modify list data as element appear in an array ; Driver Code ; creating the linked list ; Function call to sort the list in order elements are apperaing in an array ; print the modified linked list
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } ; function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } function printList ( head ) { while ( head != null ) { document . write ( head . data + " " ) ; head = head . next ; } } function sortlist ( arr , N , head ) { var hash = new Map ( ) ; var temp = head ; while ( temp != null ) { if ( hash . has ( temp . data ) ) hash . set ( temp . data , hash . get ( temp . data ) + 1 ) ; else hash . set ( temp . data , 1 ) ; temp = temp . next ; } temp = head ; for ( var i = 0 ; i < N ; i ++ ) { var frequency = hash . get ( arr [ i ] ) ; while ( frequency -- > 0 ) { temp . data = arr [ i ] ; temp = temp . next ; } } } var head = null ; var arr = [ 5 , 1 , 3 , 2 , 8 ] ; var N = arr . length ; head = push ( head , 3 ) ; head = push ( head , 2 ) ; head = push ( head , 5 ) ; head = push ( head , 8 ) ; head = push ( head , 5 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; sortlist ( arr , N , head ) ; document . write ( " " + " " ) ; printList ( head ) ;