【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) {
        if (!head || !head->next) {
            return head;
        }

        ListNode* slow = head;
        ListNode* fast = head->next;

        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
        }
        ListNode* right = sortList(slow->next);
        slow->next = nullptr;
        ListNode* left = sortList(head);

        return merge(left, right);
    }

    ListNode* merge(ListNode* l1, ListNode* l2) {
        ListNode* dummy = new ListNode(-1);
        ListNode* cur = dummy;
        while (l1 && l2) {
            if (l1->val < l2->val) {
                cur->next = l1;
                l1 = l1->next;
            } else {
                cur->next = l2;
                l2 = l2->next;
            }
            cur = cur->next;
        }

        if (l1) cur->next = l1;
        if (l2) cur->next = l2;
        return dummy->next;
    }
};

Solutin 2 ()

class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if (!head || !head->next) return head;
        ListNode *slow = head, *fast = head, *pre = head;
        while (fast && fast->next) {
            pre = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        pre->next = NULL;
        return merge(sortList(head), sortList(slow));
    }
    ListNode* merge(ListNode* l1, ListNode* l2) {
        if (!l1) return l2;
        if (!l2) return l1;
        if (l1->val < l2->val) {
            l1->next = merge(l1->next, l2);
            return l1;
        } else {
            l2->next = merge(l1, l2->next);
            return l2;
        }
    }
};
时间: 2024-10-26 03:55:34

【Lintcode】098.Sort List的相关文章

【CodeChef】Turbo Sort

题目链接:Turbo Sort 用java自带O(NlogN)的排序就可以,java要特别注意输入输出.输入用BufferedReader,输出用printWriter.printWriter的速度比System.out快很多,参考StackOverflow. 代码: 1 import java.io.BufferedOutputStream; 2 import java.io.BufferedReader; 3 import java.io.InputStreamReader; 4 impor

【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)

Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, arrays may be too large for us to wait around for insertion sort to finish. Is there some other way we can calculate the number of times Insertion Sort

【leetcode】905. Sort Array By Parity

题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数组头部,否则追加到尾部. 代码如下: class Solution(object): def sortArrayByParity(self, A): """ :type A: List[int] :rtype: List[int] """ res =

【LeetCode】排序 sort(共20题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [56]Merge Intervals [57]Insert Interval [75]Sort Colors [147]Insertion Sort List [148]Sort List [164]Maximum Gap [179]Largest Number [242]Valid Anagram [252]Meeting Rooms (2018年11月22日,为

【JavaScript】利用sort()函数与文件碎片实现表格的前端排序,兼容IE6原生态

表格排序在网页的应用也很多,尤其是一些信息系统输出一个密密麻麻的表格给人看,客户肯定会提出表格排序的要求.很多人定式思维地认为表格的排序一定要通过数据库后端进行交互,使用带order by asc/desc的语句去实现,然后再利用ajax似乎很完美似得.其实根本就不用与数据库打交道.在前端给出任意一个表格,都能够利用sort()函数与文件碎片实现表格的前端排序.在jquery里面是有一个advanceTable的插件做这件事,但是这个插件相当不好的是什么呢?与平常的插件一样,代码写得天花龙凤,没

【LeetCode】Insertion Sort List

题目 Sort a linked list using insertion sort. 解答 链表无法像数组那样从后往前依次比较插入,只能从前往后:在链表首部添加一个哨兵可以稍微简化下代码,代码如下: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * }

【Lintcode】153.Combination Sum II

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Example Given candidate set [10,1,6,7,2,1,5] 

【Lintcode】135.Combination Sum

题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Example Given candidate set [2,3,6,7] 

【转】linux sort 命令详解

sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟搞定sort,现在开始! 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. [[email protected] programming]$ cat seq.txt banana apple pear orange [[email protected] programming]$ sort seq.txt apple banana