[Leetcode] 23. Merge K Sorted Lists java__

Source: Internet
Author: User
    /**23.  Merge K Sorted Lists * @param Lists * @return merged K-Way ordered list/public ListNode mergeklists (listnode[) Lists)
        {int n = lists.length;
        if (n = = 0) return null;
        else if (n = = 1) return lists[0];
        for (int i=1; i<n; i++) {lists[0] = mergetwolists (lists[i), lists[i-1]);
    return lists[0];
        Public ListNode mergetwolists (listnode L1, ListNode L2) {listnode ret = new ListNode (-1);
        ListNode rethead = ret;
        if (L1 = = NULL && L2 = null) {return null;
        }else if (L1 = null) {return L2;
        else if (L2 = null) {return L1; 
                while (L1!= null && L2!= null) {if (L1.val <= l2.val) {ret.next = L1;
                L1 = L1.next;
            ret = Ret.next;
                else {ret.next = L2;
 L2 = L2.next;               ret = Ret.next;
        } if (L1!= null) {ret.next = L1;
        } if (L2!= null) {ret.next = L2;
    return rethead.next; }

Time limit,

    Public ListNode mergeklists (listnode[] lists) {  
        if (lists = null | | lists.length = 0) return
            null;
        Return merge (lists, 0, lists.length-1);
    }
    Public ListNode Merge (listnode[] lists, int low, int high) {
        if (Low < high) {
            int mid = (Low+high)/2;
            ListNode left = merge (lists, Low, mid);
            ListNode right = Merge (lists, mid+1, high);
            Return mergetwolists (left, right);
        return lists[low];
    }

Merging operations to reduce time complexity

The following method uses the minimum heap

    Public ListNode mergeKLists1 (listnode[] lists) {priorityqueue<listnode> heap = new Priorityqueue<l Istnode> (10,new comparator<listnode> () {@Override public int compare (Listnod  
                e N1, ListNode n2) {return n1.val-n2.val;  
        }  
        });   
            for (int i=0;i<lists.length;i++) {listnode node = lists[i];  
            if (node!=null) {Heap.offer (node);  
        } ListNode head = null;  
        ListNode pre = head;  
            while (Heap.size () >0) {ListNode cur = heap.poll ();  
                if (head = = null) {head = cur;  
            Pre = head;  
            else {pre.next = cur;  
            Pre = cur;  
             if (cur.next!=null)   Heap.offer (Cur.next);  
    return head;  }

Heap
maintains a heap with a size of K, each time the smallest element on the top of the heap is placed in the result, and the next element is read into the heap to be maintained again.
Because each list is ordered, each time it goes to the smallest of the current k elements, so when all the linked lists are finished, all the elements are placed in the result list from small to large.
This algorithm reads once for each element, that is k*n times, and then each time the element is read to insert the new element into the heap to logk the complexity,
so the total time complexity is O (NKLOGK). Space complexity is the size of the heap, that is, O (k).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.