[Lintcode]98. Sort List/[Leetcode]148. Sort List

98. Sort List/148. Sort List

  • 本题难度: Medium
  • Topic: Linked List

Description

Sort a linked list in O(n log n) time using constant space complexity.

Example

Example 1:

Input: 1->3->2->null

Output: 1->2->3->null

Example 2:

Input: 1->7->2->6->null

Output: 1->2->6->7->null

Challenge

Solve it by merge sort & quick sort separately.

Challenge

Can you do this in-place without altering the nodes‘ values?

我的代码

class Solution(object):
    def merge(self,x,y):
        pos = ListNode(0)
        res = pos
        while(x and y):
            if x.val<y.val:
                pos.next = x
                x = x.next
            else:
                pos.next = y
                y = y.next
            pos = pos.next
        pos.next = x or y
        return res.next

    def sortList(self,head):
        if not head or not head.next:
            return head

        pre,slow,fast = None, head,head
        while fast and fast.next:
            pre,slow,fast = slow,slow.next,fast.next.next
        pre.next = None# 保证第一列始终不长于第二列

        return self.merge(*map(self.sortList,(head,slow)))

思路

采用归并排序

  • 时间复杂度 O(nlog(n))

原文地址:https://www.cnblogs.com/siriusli/p/10376473.html

时间: 2024-10-06 13:09:00

[Lintcode]98. Sort List/[Leetcode]148. Sort List的相关文章

Leetcode 148. Sort List 归并排序 in Java

148. Sort List Total Accepted: 81218 Total Submissions: 309907 Difficulty: Medium Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; *

[LeetCode] 148. Sort List 解题思路

Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn),额外空间为 O(1). O(n*logn) 时间排序算法,无法是 quick sort, merge sort, head sort.quick sort 需要灵活访问前后元素,适合于数组,merge sort 只需要从左到右扫过去即可,可用于列表结构. 当列表元素个数大于2时,将列表拆分为左右

[LeetCode#148]Sort List

The problem: Sort a linked list in O(n log n) time using constant space complexity. My analysis: The idea behind this problem is easy : merge sort !But we should learn some tricky skills from this question. 1. How to split a linked list into two sepa

Java for LeetCode 148 Sort List

Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log n),由于优先级队列是用堆排序实现的,因此,我们使用优先级队列即可,JAVA实现如下: public ListNode sortList(ListNode head) { if(head==null||head.next==null) return head; Queue<ListNode> pQ

leetcode 148. Sort List ----- java

Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复杂度,那么就使用归并就可以了. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ p

[LeetCode] 148. Sort List 链表排序

Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2->1->3 Output: 1->2->3->4 Example 2: Input: -1->5->3->4->0 Output: -1->0->3->4->5 解法:归并排序.由于有时间和空间复杂度的要求.把链表从中间分开,递归下去,都

[LeetCode]148. Sort List链表归并排序

要求时间复杂度O(nlogn),空间复杂度O(1),采用归并排序 传统的归并排序空间复杂度是O(n),原因是要用一个数组表示合并后的数组,但是这里用链表表示有序链表合并后的链表,由于链表空间复杂度是O(1),所以可以. 链表问题经常出现TLE问题或者MLE问题,这时候要检查链表拼接过程或者循环过程,看有没有死循环 public ListNode sortList(ListNode head) { if (head==null||head.next==null) return head; //利用

排序算法之快速排序(Quick Sort) -- 适用于Leetcode 75 Sort Colors

Quick Sort使用了Divide and Concur的思想: 找一个基准数, 把小于基准数的数都放到基准数之前, 把大于基准数的数都放到基准数之后 Worst case: O(n^2) Average case: O(nlogN) 步骤: 初始的数组 Array a[]: 0 1 2 3 4 5 6 7 8 9 51 73 52 18 91 7 87 73 48 3 基准数: X = a[0] = 51 i 的值: i = 0 j 的值: j = 9 (a.length) Step 1:

LeetCode Insertion Sort List

class Solution { public: ListNode *insertionSortList(ListNode *head) { if (head == NULL) return NULL; ListNode* sorted_head = head; ListNode* unsorted_head = head->next; head->next = NULL; ListNode* cur = unsorted_head; while (cur != NULL) { unsorte