sort-list

算法:在O(n log n)的时间内使用常数级空间复杂度对链表进行排序

package com.niuke.p4;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode sortList (ListNode head) {
        // write code here
        if(head == null ||head.next==null) {
            return head;
        }
        
        //获取中间节点
        ListNode mid = getMiddle(head);
        //断开
        ListNode midNext = mid.next;
        mid.next = null;
        
        //排序,合并
        return mergeTwoLists(sortList(head),sortList(midNext));
    }
    
    /**
     * 获取链表的中间节点,偶数时取中间第一个
     * @param head
     * @return
     */
    public ListNode getMiddle(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        
        //快慢指针
        ListNode fast,slow;
        fast = head;
        slow = head;
        //快两步,慢一步
        while(fast.next != null && fast.next.next != null) {
            //偶数时取第一个
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    
    /**
     * 实现合并两个已经排序的链表
     * @param l1
     * @param l2
     * @return
     */
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        //特殊情况
        if(l1 == null) {
            return l2;
        }
        if(l2 == null) {
            return l1;
        }
        ListNode first = l1.next,second=l2.next;
        ListNode res,newHead;//结果
        if(l1.val <l2.val) {
            newHead = res = l1;
            second = l2;
        }else {
            newHead = res = l2;
            first = l1;
        }
        while(first != null || second != null) {
            if(first == null) {//第一条链表没了
                res.next = second;
                return newHead;
            }else if(second == null) {//第二条链表没了
                res.next = first;
                return newHead;
            }else if(first.val < second.val) {//第一个值小
                res.next = first;
                first = first.next;
                res = res.next;
            }else {
                res.next = second;
                second = second.next;
                res = res.next;
            }
        }
        return newHead;
    }
}
原文地址:https://www.cnblogs.com/LoganChen/p/12990042.html