在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
示例 1:
输入: 4->2->1->3 输出: 1->2->3->4
示例 2:
输入: -1->5->3->4->0 输出: -1->0->3->4->5
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode sortList(ListNode head) { if(head == null || head.next == null) return head; ListNode fast = head, slow = head, pre = slow; while(fast != null && fast.next != null) { pre = slow; slow = slow.next; fast = fast.next.next; } pre.next = null; ListNode l = sortList(head); ListNode r = sortList(slow); return merge(l, r); } private ListNode merge(ListNode l, ListNode r) { ListNode q = new ListNode(0), p = q; while(l != null && r != null) { if(l.val < r.val) { p.next = l; l = l.next; } else { p.next = r; r = r.next; } p = p.next; } if(l != null) p.next = l; if(r != null) p.next = r; return q.next; } }