和数组里面的归并排序思想一致
class Solution {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 rightHead = slow.next;slow.next = null;ListNode left = sortList(head);ListNode right = sortList(rightHead);return mergeList(left, right);}public ListNode mergeList(ListNode l1, ListNode l2){if(l1 == null)return l2;if(l2 == null)return l1;ListNode dummyNode = new ListNode(-1);ListNode curr = dummyNode;while (l1 != null && l2 != null){if(l1.val <= l2.val){curr.next = l1;l1 = l1.next;}else {curr.next = l2;l2 = l2.next; }curr = curr.next;}if(l1 == null)curr.next = l2;if(l2 == null)curr.next = l1;return dummyNode.next;}}