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

原创 2015年07月06日 23:17:32
Merge k Sorted Lists 

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.


思路:此题是由合并两个排序链表演化而来,刚开始,想法比较简单,像求最大公共前缀一样,逐一求解;但是最后超时,所以马上意识到出题方是为了使用归并和分治的方法,故重新写了代码。

代码一(超时未过):

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        int len = lists.length;
        if(len == 0){
            return null;
        }
        if(len == 1){
            return lists[0];
        }
        ListNode head = lists[0];
        for(int i = 0; i < len; i++){
            head = mergeTwoLists(head,lists[i]);
        }
        return head;
    }
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null || l2 == null)
            return l1 == null ? l2:l1;
        ListNode head = new ListNode(0);//定义一个头结点
        ListNode p = head;
        ListNode temp = null;
        
        while(l1 != null && l2 != null){
            if(l1.val > l2.val){
                temp = l2;//用一个temp保存现在的l1或l2
                l2 = l2.next;//l1或l2指针后移1位
            }else{
                temp = l1;
                l1 = l1.next;
            }//交换数据
            p.next = temp;
            p = p.next;
        }//temp取不为空的一个(也可能全为空)
        temp = l1 == null ?l2:l1;
        p.next = temp;//将剩余的全部链接即可(上面的方法太啰嗦了)
        return head.next;
    }
}
代码二(通过):

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        int len = lists.length;
        //长度分别为<=1时的情况
        if(len == 0){
            return null;
        }
        if(len == 1){
            return lists[0];
        }
        ListNode[] ln = lists;
        while(len > 2){//大于等于2分而治之
            ln = merge(ln);//调用函数
            len = ln.length;//更新len
        }//最后两个归并
        ln[0] = mergeTwoLists(ln[0],ln[1]);
        return ln[0];
    }
    //对lists两两归并
    public ListNode[] merge(ListNode[] lists){
        int len = lists.length;
        //两两归并
        ListNode[] arr;
        //长度为偶数
        if((len & 1) == 0){
            arr = new ListNode[len/2];
        }else{//长度为奇数
            arr = new ListNode[len/2 + 1];
            arr[len/2] = lists[len - 1];//最后1位不参与归并
        }//两两归并实现;i < len-1;不能是i<len;不然报错
        for(int i = 0; i < len - 1; i = i+2){
        	ListNode a = lists[i];
        	ListNode b = lists[i+1];
            arr[i/2] = mergeTwoLists(lists[i],lists[i+1]);//调用两两归并方法
        }
        return arr;
    }
    
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null || l2 == null)
            return l1 == null ? l2:l1;
        ListNode head = new ListNode(0);//定义一个头结点
        ListNode p = head;
        ListNode temp = null;
        
        while(l1 != null && l2 != null){
            if(l1.val > l2.val){
                temp = l2;//用一个temp保存现在的l1或l2
                l2 = l2.next;//l1或l2指针后移1位
            }else{
                temp = l1;
                l1 = l1.next;
            }//交换数据
            p.next = temp;
            p = p.next;
        }//temp取不为空的一个(也可能全为空)
        temp = l1 == null ?l2:l1;
        p.next = temp;//将剩余的全部链接即可(上面的方法太啰嗦了)
        return head.next;
    }
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode解题之23.Merge k Sorted Lists Java版本(合并k个有序的链表)

leetcode解题之23.Merge k Sorted Lists Java版本(合并k个有序的链表)

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

Merge Two Sorted Lists  Merge two sorted linked lists and return it as a new list. The new list sho...

Leetcode 23. Merge k Sorted Lists合并k个排序链表

题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity...

LeetCode 23 Merge k Sorted Lists(合并K个已排序链表)

翻译合并K个已排序的链表,并且将其排序并返回。 分析和描述其复杂性。原文Merge k sorted linked lists and return it as one sorted list. An...
  • NoMasp
  • NoMasp
  • 2015年11月12日 17:30
  • 3131

leetcode_效率题解_23. Merge k Sorted Lists(合并k个有序链表)

相关题解: leetcode_效率题解_[python/C++]_21. Merge Two Sorted Lists(合并2个有序链表)题目链接 【题目】 Merge k sorted lin...

LeetCodet题解--23. Merge k Sorted Lists(合并K个已排序的链表)

链接 LeetCode题目:https://leetcode.com/problems/generate-parentheses GitHub代码:https://github.com/...
  • gatieme
  • gatieme
  • 2016年04月08日 16:32
  • 1503

[leetcode] 23. Merge k Sorted Lists 解题报告

题目链接:https://leetcode.com/problems/merge-k-sorted-lists/ Merge k sorted linked lists and return it ...

LeetCode23 Merge k Sorted Lists 把K个有序链表连接成一个

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its comple...

[leetcode-排序]--23. Merge k Sorted Lists

Question 23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. An...

23. Merge k Sorted Lists 合并k个链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetCode 23. Merge k Sorted Lists (合并k个排序链表) 解题思路和方法
举报原因:
原因补充:

(最多只允许输入30个字)