148. 排序链表
归并排序
class Solution { public ListNode sortList(ListNode head) { if(head == null || head.next == null) return head; ListNode fast = head,slow = head; while(fast.next != null && fast.next.next != null) { fast = fast.next.next; slow = slow.next; } ListNode next = slow.next; slow.next = null; ListNode left = sortList(head); ListNode right = sortList(next); return merge(left,right); } public ListNode merge(ListNode l, ListNode r) { if(l == null) return r; if(r == null) return l; if(l.val < r.val) { l.next = merge(l.next,r); return l; } r.next = merge(l,r.next); return r; } }