LeetCode, leetcodeoj

Source: Internet
Author: User

LeetCode, leetcodeoj

Question:

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

Ideas:

1) The Code of Merge two sorted lists is used directly, but it times out. The reason is that this time is too complex, 2n + 3n + 4n +... + kn = O (nk ^ 2)

package list;public class MergeKSortedLists {    public ListNode mergeKLists(ListNode[] lists) {        int len;        if (lists == null || (len = lists.length) == 0) return null;        ListNode q = lists[0];        for (int i = 1; i < len; ++i) {            q= mergeTwoLists(q, lists[i]);        }                return q;    }        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {                ListNode p = new ListNode(0);        p.next = null;        ListNode head = p;                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 ? l2 : l1;        return head.next;    }        public static void main(String[] args) {        // TODO Auto-generated method stub    }}

2) use merge sort to merge data. The time complexity is 2n * k/2 + 4n * k/4 + .. + (2 ^ x) n * k/(2 ^ x) = xkn, where x is logk

package list;public class MergeKSortedLists {    public ListNode mergeKLists(ListNode[] lists) {        int len;        if (lists == null || (len = lists.length) == 0) return null;                int cur = 0;        int end = len - 1;        while (end > 0) {            cur = 0;            while (cur < end) {                lists[cur] = mergeTwoLists(lists[cur], lists[end]);                ++cur;                --end;            }        }                            return lists[0];    }        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {                ListNode p = new ListNode(0);        p.next = null;        ListNode head = p;                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 ? l2 : l1;        return head.next;    }        public static void main(String[] args) {        // TODO Auto-generated method stub    }}

 

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.