import { ListNode } from './LinkList' ; param { ListNode } head return { ListNode } function sortList ( head ) { if ( head === null || head . next === null ) { return head ; } return cut ( head ) ; } ; param { ListNode } head return { ListNode } const cut = function ( head ) : ListNode { if ( head === null || head . next === null ) { return head ; } let slow = head , fast = head ; while ( fast !== null ) { fast = fast . next ; fast = fast !== null ? fast . next : null ; if ( fast !== null ) { slow = slow . next ; } } let half = slow . next ; slow . next = null ; let left = cut ( head ) , right = cut ( half ) ; return merge ( left , right ) ; } ; param { ListNode } left param { ListNode } right const merge = function ( left , right ) : ListNode { let result = null ; let root = result ; while ( left !== null || right !== null ) { if ( left === null ) { root . next = right ; break ; } else if ( right === null ) { root . next = left ; break ; } let temp = null ; if ( left . val > right . val ) { temp = right ; right = right . next ; } else { temp = left ; left = left . next ; } if ( result === null ) { result = temp ; root = result ; } else { root . next = temp ; root = root . next ; } } return result ; } ; export { sortList } ;