【链表】Sort List(归并排序)

题目:

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

思路:

nlogn的排序有快速排序、归并排序、堆排序。双向链表用快排比较适合,堆排序也可以用于链表,单向链表适合用归并排序。

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var sortList = function(head) {
    if(head==null||head.next==null){
        return head;
    }else{
        var slow=head,fast=head;
        while(fast.next!=null&&fast.next.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        //拆成两个链表
        fast=slow;
        slow=slow.next;
        fast.next=null;

        fast=sortList(head);
        slow=sortList(slow);
        return merge(fast,slow);
    }
};

function merge(head1,head2){
    if(head1==null){
        return head2;
    }
    if(head2==null){
        return head1;
    }
    var res=new ListNode(),p=new ListNode();
    if(head1.val<head2.val){
        res=head1;
        head1=head1.next;
    }else{
        res=head2;
        head2=head2.next;
    }
    p=res;

    while(head1!=null&&head2!=null){
        if(head1.val<head2.val){
            p.next=head1;
            head1=head1.next;
        }else{
            p.next=head2;
            head2=head2.next;
        }
        p=p.next;
    }

    if(head1!=null){
        p.next=head1;
    }else if(head2!=null){
        p.next=head2;
    }

    return res;
}
时间: 2024-08-10 02:11:49

【链表】Sort List(归并排序)的相关文章

[C++]LeetCode: 125 Sort List (归并排序链表)

题目:Sort a linked list in O(n log n) time using constant space complexity. 思路:题目要求我们使用常数空间复杂度,时间复杂度为O(nlog(n)). 满足这个时间复杂度的有快速排序,归并排序,堆排序.插入排序时间复杂度为O(n^2). 双向链表用快排比较合适,堆排序也可用于链表,单项链表适合于归并排序.我们就用归并排序的思想来完成链表的排序. 首先是用快慢双指针找到链表中间的位置,然后分成前后端分别递归的归并排序,最后合并.

两个有序链表连接(归并排序中用到的)

刚才写了k个,顺手写个2个的,在链表的归并排序中很有用,效率非常好 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * } 10 * } 11 */ 12 public class Solution { 13 public Lis

链表排序之归并排序

链表排序之归并排序: public static ListNode mergeSortList(ListNode head){ if (null == head || null == head.next){ return head; } ListNode slow = head; ListNode fast = head; ListNode pre = head; while (null != fast && null != fast.next){ pre = slow; slow = s

148 Sort List 链表上的归并排序和快速排序

在使用O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 详见:https://leetcode.com/problems/sort-list/description/ 方法一:归并排序 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class

写给自己看的单链表(5):归并排序

搬运自我的CSDN https://blog.csdn.net/u013213111/article/details/88670270 !!!Attention:以下操作中的单链表均带有头结点!!!参考怎样实现链表的归并排序 由于待处理的单链表带有头结点,因此把程序分为MergeSort和MergeSortCore两部分,其中MergeSort只是用来处理头结点的,这与写给自己看的单链表(2):进阶操作中的合并程序类似. 从MergeSortCore的伪代码可以一窥归并排序的思路: 1 Lnod

排序 起泡排序(bubble sort),归并排序(merge sort)

1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换,就说明前区已经有序了,直接终止了.但是有个效率低下的地方,就是右边界hi是每次循环向前移动一个单元 跳跃版,在提前终止版的基础上,解决右边界hi移动效率低下的问题.解决思路:每次循环后,记录下最后一次的交换位置A,然后让hi=交换位置A,所以hi就可以跳跃移动多个单元了. 基本版代码实现 //冒泡排序(基本版 效率低下) template<typename T> void Vect

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; *

Natural Merge Sort(自然归并排序)

This is a Natural Merge Sort program from my textbook. It works, but I don't think it's good. // Natural merge sort program in the textbook public class NaturalMergeSortProgram { public static void main(String[] args) { int a[] = new int[10000000]; i

UVALive 6604 Airport Sort 【归并排序】【逆序数】

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4615 题目大意:T代表T组样例,n个人以及k组为一个区域,然后接下来n个数表示n个人的位置,然后是两个置换: 每次只能交换相邻的两个人,每交换一次花费一秒.求出一个交换到要求的位置后花费的时间t1. 可以直接交换两个人的位置,花费的时间是两个人现在的位置差,可以同时

【LeetCode】 sort list 单链表的归并排序

题目:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排序,链表找到中点的方法 存在的缺点:边界条件多考虑!!! /** * LeetCode Sort List Sort a linked list in O(n log n) time using constant space complexity. * 题目:将一个单链表进行排序,时间复杂度要求为o