public class leetcode148 { public static class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public static void main(String[] args) { } public ListNode sortList(ListNode head) { if(head == null || head.next == null) return head; ListNode slow = head; ListNode fast = head.next; while(fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } ListNode temp = slow.next; slow.next = null; ListNode left = sortList(head); ListNode right = sortList(temp); ListNode h = new ListNode(0); ListNode res = h; while(left != null && right != null) { if(left.val <= right.val) { h.next = left; left = left.next; } else { h.next = right; right = right.next; } h = h.next; } h.next = left== null? right : left; return res.next; } }