Function to merge sort
function mergeSort ( head ) { if ( head . next == null ) return head ; var mid = findMid ( head ) ; var head2 = mid . next ; mid . next = null ; var newHead1 = mergeSort ( head ) ; var newHead2 = mergeSort ( head2 ) ; var finalHead = merge ( newHead1 , newHead2 ) ; return finalHead ; }