Sort List

2015-11-23  本文已影响83人  ab409

Sort List


今天是一道有关排序的题目,来自LeetCode,难度为Medium,Acceptance为27%

题目如下

Sort a linked list in O(n log n) time using constant space complexity.
Example
Given 1-3->2->null, sort it to 1->2->3->null.

解题思路及代码见阅读原文

回复0000查看更多题目

解题思路

首先,看到排序我们先来回忆一下学过了排序算法。

那么,这里的算法对于数组都适用,但对于单向链表不一定适用。在该题中要求时间复杂度为O(nlogn),因此我们可以从快速排序归并排序堆排序中选择,看看有没有一种适合这里的情况。

下面我们来看代码。

代码如下

Java版

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param head: The head of linked list.
     * @return: You should return the head of the sorted linked list,
                    using constant space complexity.
     */
    public ListNode sortList(ListNode head) {  
        // write your code here
        return mergeSort(head);
    }
    
    private ListNode mergeSort(ListNode head) {
        if(null == head || null == head.next)
            return head;
            
        ListNode fast = head, slow = head, prevSlow = slow;
        while(fast != null && fast.next != null) {
            prevSlow = slow;
            fast = fast.next.next;
            slow = slow.next;
        }
        prevSlow.next = null;
        ListNode head2 = slow;
        head = mergeSort(head);
        head2 = mergeSort(head2);
        return merge(head, head2);
    }
    
    private ListNode merge(ListNode head1, ListNode head2) {
        if(null == head1)
            return head2;
        if(null == head2)
            return head1;
        ListNode head = new ListNode(0), p = head;
        while(head1 != null && head2 != null) {
            if(head1.val < head2.val) {
                p.next = head1;
                head1 = head1.next;
                p = p.next;
            } else {
                p.next = head2;
                head2 = head2.next;
                p = p.next;
            }
        }
        if(head1 != null)
            p.next = head1;
        if(head2 != null)
            p.next = head2;
        return head.next;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读