[leedcode 23] Merge k Sorted Lists

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    //本题有两种解法,一种是所有的链表一起进行比较,因为每个节点平均需要比较k次,一共kn个节点所以时间复杂度是O(knk)
    //第二种借鉴了二分排序法,T(k)=2T(k/2)+O(nk);时间复杂度是O(nklogk)
    public ListNode mergeKLists(ListNode[] lists) {
        if(lists.length<1) return null;
        if(lists.length==1) return lists[0];
        return merge1(lists,0,lists.length-1);
    }
    public ListNode merge1(ListNode[] lists,int start,int end){

        if(start<end){
            int mid=(start+end)/2;
            ListNode l1=merge1(lists,start,mid);
            ListNode l2=merge1(lists,mid+1,end);
            return merge2(l1,l2);//二分排序
        }else{
            return lists[start];
        }
    }

    public ListNode merge2(ListNode l1,ListNode l2){
        ListNode newHead=new ListNode(-1);
        ListNode temp=newHead;
        while(l1!=null&&l2!=null){
            if(l1.val<l2.val){
                temp.next=l1;
                l1=l1.next;

            }else{
                temp.next=l2;
                l2=l2.next;
            }
            temp=temp.next;

        }
        if(l1!=null){
            temp.next=l1;
        }
        if(l2!=null){
            temp.next=l2;
        }
        return newHead.next;

    }
}
时间: 2024-10-29 10:46:11

[leedcode 23] Merge k Sorted Lists的相关文章

[LeetCode]23. Merge k Sorted Lists

23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 给定k个排序了的链表,合并k个链表成一个排序链表. 本程序思路: 1)首先得到K个链表的长度和存在len中 2)从K个链表中找到值最小的那个节点,把该节点添加到合并链表中 3)重复len次即可把所有节点添加到合并链表中. 注意事项: 1)K个链表中有的

#23 Merge k Sorted Lists (N路归并排序)

#23 Merge k Sorted Lists (N路归并排序) 题目地址:#23 题目分类:链表/归并排序/堆排序 题目难度:hard 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 翻译:合并K个已经排序的链表,返回一个排序好的链表. 思路 暴力法,我们很容易想到:以一个for循环遍历所有的链表,找出最小的,然后将这个节点加入新的链表

23. Merge k Sorted Lists - LeetCode

Question 23.?Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 Java实现: public ListNode mergeKLists(ListNode[] lists) { ListNode preHead = new ListNode(0); ListNode minNode = getMinNode(lists); ListNode tmpNode =

23. Merge k Sorted Lists(js)

23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [   1->4->5,   1->3->4,   2->6 ] Output: 1->1->2->3->4->4->5->6题意:按大小顺序合并n条链表代码

[Leetcode][Python]23: Merge k Sorted Lists

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 23: Merge k Sorted Listshttps://oj.leetcode.com/problems/merge-k-sorted-lists/ Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. ===Comments by

leetCode 23. Merge k Sorted Lists (合并k个排序链表) 解题思路和方法

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:此题是由合并两个排序链表演化而来,刚开始,想法比较简单,像求最大公共前缀一样,逐一求解:但是最后超时,所以马上意识到出题方是为了使用归并和分治的方法,故重新写了代码. 代码一(超时未过): /** * Definition for singly-link

23. Merge K Sorted Lists (Java, 归并排序的思路)

题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解析:合并k个已经有序的单链表,使其最终成为一个有序的单链表.原理就是归并排序,递归运算.基本算法recusion 与 merge 编码: public ListNode mergeKLists(ListNode[] lists) { if(lists == null || lists.leng

[LeetCode] 23. Merge k Sorted Lists ☆☆

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解法1: 采用递归的方法,不管合并几个,归根到底还是需要两两合并. 首先想到的是前两个先合并,然后再跟第三个合并,然后第四个....但是这种做法效率不高. 换个思路,采用分治法,对数量超过2的任务进行拆分,直到最后只有一个或两个链表再进行合并.代码如下: /** * Definition for si

23. Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Subscribe to see which companies asked this question 思路:类似归并排序,每个链表已经排好序了,现在只需要将各个链表合并成一个链表.要点:分而治之,最后合并. /** * Definition for singly-linked list. * st