[LeetCode]23. Merge k Sorted Lists合并K个排序链表

Posted jchen104

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode]23. Merge k Sorted Lists合并K个排序链表相关的知识,希望对你有一定的参考价值。

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

要合并K个排好序的链表,我用的方法是用一个优先队列每次存K个元素在队列中,根据优先队列的属性把小的数放在最前面,这样每次
取出队首就是我们要的,这样把K个链表全部过一遍优先队列就得到了排好序的新序列,不了解优先队列的可以看这里http://www.importnew.com/6932.html
(这里比较奇怪的是我自己的从测试代码是没问题的,第一个测试样例过了但是提交却显示)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {        
        Comparator<ListNode> idComparator=new Comparator<ListNode>() {           
            @Override
            public int compare(ListNode l1,ListNode l2) {
                return l1.val-l2.val;
            }            
        };        
        int len=lists.length;
        Queue<ListNode> q=new PriorityQueue<ListNode>(len,idComparator);
        for(int i=0;i<len;i++) {
            if(lists[i]!=null) q.add(lists[i]);
        }
        ListNode res=new ListNode(0), head=res,temp=null;
        while(!q.isEmpty()) {
            temp=q.poll();
            head.next=temp;
            head=head.next;
            if(temp.next!=null) q.add(temp.next);
        }
        return res.next;
    }
}

下面是我的测试代码

public class Test {        
    
    public static void main(String[] args) {            
        Random rand=new Random();
        int max=0;
        ListNode t1=new ListNode(-1);
        ListNode l1=t1;    
        int a=0;
        for(int i=0;i<5;i++) {
            a=rand.nextInt(100);
            if(a>max) max=a;
            t1.next=new ListNode(max);
            t1=t1.next;
        }
        
        ListNode t2=new ListNode(-1);
        ListNode l2=t2;
        int b=0;
        max=b;
        for(int i=0;i<7;i++) {
            b=rand.nextInt(100);
            if(b>max) max=b;
            t2.next=new ListNode(max);
            t2=t2.next;
        }
        
        ListNode t3=new ListNode(-1);
        ListNode l3=t3;    
        int c=0;
        max=c;
        for(int i=0;i<3;i++) {
            c=rand.nextInt(100);
            if(c>max) max=c;
            t3.next=new ListNode(max);
            t3=t3.next;
        }
        
        ListNode[] lists= {l1.next,l2.next,l3.next};
        ListNode temp=new ListNode(-1);
        
        Solution s=new Solution();
        temp=s.mergeKLists(lists);
        
        while(temp!=null) {
            System.out.print(temp.val+"--->");
            temp=temp.next;
        }
    }    
}

class ListNode {
     int val;
     ListNode next;
     ListNode(int x) { 
         val = x; 
    }
 }
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {        
        Comparator<ListNode> idComparator=new Comparator<ListNode>() {           
            @Override
            public int compare(ListNode l1,ListNode l2) {
                return l1.val-l2.val;
            }            
        };        
        int len=lists.length;
        Queue<ListNode> q=new PriorityQueue<ListNode>(len,idComparator);
        for(int i=0;i<len;i++) {
            if(lists[i]!=null) q.add(lists[i]);
        }
        ListNode res=new ListNode(0), head=res,temp=null;
        while(!q.isEmpty()) {
            temp=q.poll();
            head.next=temp;
            head=head.next;
            if(temp.next!=null) q.add(temp.next);
        }
        return res.next;
    }
}

我自己测试是没有问题的,但是提交不过也没弄明白

 

下面是c++版本的代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
struct cmp {
    bool operator () (ListNode *a, ListNode *b) {
        return a->val > b->val;
    }
};
 
class Solution {  
public:  
    ListNode *mergeKLists(vector<ListNode *> &lists) {  
        priority_queue<ListNode*, vector<ListNode*>, cmp> q;
        for (int i = 0; i < lists.size(); ++i) {
            if (lists[i]) q.push(lists[i]);
        }
        ListNode *head = NULL, *pre = NULL, *tmp = NULL;
        while (!q.empty()) {
            tmp = q.top();
            q.pop();
            if (!pre) head = tmp;
            else pre->next = tmp;
            pre = tmp;
            if (tmp->next) q.push(tmp->next);
        }
        return head;
    }  
};  

优先队列的底层是通过的小根堆实现的,通过compare把小的元素放在前面,取出队首后维护这个小根堆,元素加入堆中的复杂度为O(longk),总共有kn个元素加入堆中,因此,是O(nklogk)

 

以上是关于[LeetCode]23. Merge k Sorted Lists合并K个排序链表的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

[LeetCode]23. Merge k Sorted Lists

leetcode23. Merge k Sorted Lists

leetCode 23. Merge k Sorted Lists

leetcode 23. Merge k Sorted Lists

LeetCode-23-Merge k Sorted Lists