[LintCode] Sort List

Sort List

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.

SOLUTION:

这题是merge 2 sorted list的follow up,为什么这么说呢?因为在链表上做nlogn的排序,只有merge sort,同样的时间复杂度,也有heap sort,但是不太合适。

既然这样明确了merge sort,那么就开始,直接上递归就很方便!递归的时候需要中点,这个很麻烦,链表找中点是链表操作里比较费事儿的,但是用两个指针轻松解决,写一个findmid();,然后merge左右两边,也就是分治,left = sortlist(head),right = sortlist(mid)就ok了,分治的核心就是这一层看着一层,其他的事情交给其他层,这层就当他已经处理完了。

/**
 * 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.
     */
    //思路就是:分治左右两边,然后递归,然后再把左右两边当作sorted listmerge一下
    //用任何递归的想法都是,我写了这个公式的意思就是我已经排序好了。剩下的计算机自己干去
    public ListNode sortList(ListNode head) {
        //值得注意的地方,第一次没写head.next ==null的情况
        if (head == null || head.next ==null){
            return head;
        }
        ListNode preMid = findMid(head);
        ListNode right = preMid.next;
        preMid.next = null;
        ListNode left = sortList(head);
        right = sortList(right);
        return merge(left, right);
    }
    private ListNode findMid(ListNode head){
        if (head == null){
            return head;
        }
        ListNode fast = head.next;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    private ListNode merge(ListNode l1, ListNode l2){
        if (l1 == null){
            return l2;
        }
        if (l2 == null){
            return l1;
        }
        ListNode dummy = new ListNode(0);
        ListNode head = dummy;
        while (l1 != null && l2 != null){
            if (l1.val < l2.val){
                head.next = l1;
                head = head.next;
                l1 = l1.next;
            } else {
                head.next = l2;
                head = head.next;
                l2 = l2.next;
            }
        }
        if (l1 != null){
            head.next = l1;
        }
        if (l2 != null){
            head.next = l2;
        }
        return dummy.next;
    }
}

时间: 2024-10-05 16:18:19

[LintCode] Sort List的相关文章

Lintcode: Sort Colors II 解题报告

Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. 注意

[LintCode] Sort List 链表排序

Sort a linked list in O(n log n) time using constant space complexity. Have you met this question in a real interview? Yes Example Given 1->3->2->null, sort it to 1->2->3->null. Challenge Solve it by merge sort & quick sort separatel

[LintCode] Sort Integers 整数排序

Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort or any O(n2) algorithm. Example Given [3, 2, 1, 4, 5], return [1, 2, 3, 4, 5]. 这道题让我们实现最基本的几个O(n2)的排序算法,选择排序,冒泡排序和插入排序,都是最基本的排序算法.我们一个一个来看,首先来看冒泡排序,算法

[LintCode] Sort Integers II 整数排序之二

Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(nlogn) algorithm. Example Given [3, 2, 1, 4, 5], return [1, 2, 3, 4, 5]. 解法一: // Quick sort class Solution { public: /** * @param A an integer array *

LintCode: Sort Colors

通过交换,对0,1,2排序 使用三个标记[循环不变式] i从前向后,记录最后一个0的位置 j从后向前,记录第一个2的位置 k从前向后,是遍历用的游标 [0..i-1]是0 [i..k-1]是1 [k,j-1]是未探测 [j..n-1]是2 初始k=0时,0,1,2的区域都是空,所有区域都是未探测,循环k=0..n-1 如果a[k] = 0=>swap(a[i++], a[k]) 如果a[k] = 1=>无操作 如果a[k] = 2=>swap(a[--j], a[k--]) 复杂度O(n

LintCode 463 Sort Integer

这个是O(n2)的排序的总结 /* bubble sort */public static void sortIntegers(int[] A) { // Write your code here int len = A.length; if (len == 0) return; for (int j = 0; j < len; ++j) { for (int i = 0; i < len-1-j; ++i) { // len-1-j if (A[i] > A[i+1]) { int t

lintcode 中等题:sort letters by case字符大小写排序

题目 字符大小写排序 给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序. 您在真实的面试中是否遇到过这个题? Yes 样例 给出"abAcD",一个可能的答案为"acbAD" 注意 小写字母或者大写字母他们之间不一定要保持在原始字符串中的相对位置. 挑战 在原地扫描一遍完成 解题 这个题目很简单,前面刚做一个把大于某个数之和的排在后面,快速排序的思想 public class Solution { /** *@param chars: The le

【Lintcode】098.Sort List

题目: 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. 题解: O(n log n) : 快速排序,归并排序,堆排序 Solution 1 () class Solution { public: ListNode *sortList(ListNode *head) {

Sort Colors II Lintcode

Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. Notice You are not suppose to use the library's sort function for this pro