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.length == 0)
           return null;
        if(lists.length == 1)
           return lists[0];
        return recursion(lists,0,lists.length - 1);
    }
    //recursion
    public ListNode recursion(ListNode[] lists,int start,int end){
        if(start == end)//只有一个链表
           return lists[start];
        if(start < end){
            int mid = start + (end - start) / 2; //注意:这里防止整数越界的处理,start+(start+end)/2
            ListNode l1 = recursion(lists,start,mid);
            ListNode l2 = recursion(lists,mid + 1,end);
            return merge(l1,l2);
        } else
            return null;

    }
    //merge
    public ListNode merge(ListNode l1,ListNode l2){
        ListNode head = new ListNode(0); //创建一个头结点,最后还要删掉
        ListNode p = head;
        while(l1 != null && l2 != null){
            if(l1.val <= l2.val){
                p.next = l1;
                l1 = l1.next;
            } else{
                p.next = l2;
                l2 = l2.next;
            }
            p = p.next;
        }

        p.next = (l1 != null) ? l1 : l2;
        return head.next;// head的下一个节点是第一个数据结点
    }
时间: 2024-10-10 02:26:30

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

23. Merge k Sorted Lists (JAVA)

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 /** * Definition for singly-linked list.

#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 =

[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(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

LeetCode 23 Merge k Sorted Lists (C,C++,Java,Python)

Problem: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Solution: 采用胜者树的方法,胜者树介绍:胜者树,假如链表数组长度为n,链表元素总个数为k,那么时间复杂度为O(k*log(n)) 题目大意: 给定一个数组有序链表(数组元素可能为空),要求将这些链表合并为一个有序的链表. Java源代码(522ms): /** * Defi

Java [leetcode 23]Merge k Sorted Lists

题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路: 分而自治方法.将K个List不断地分解为前半部分和后半部分.分别进行两个List的合并.最后将合并的结果合并起来. 代码如下: /** * Definition for singly-linked list. * public class ListNode { * int va