"Leetcode" Merge k Sorted Lists Problem Solving report

Source: Internet
Author: User

Topic

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

Combine several ordered linked lists into one, and analyze the complexity of the algorithm.


"Divide and Conquer"

The intuitive idea is that 22 merges, there are two ways: 1) list1 and List2 merge for Newlist2,newlist2 and List3 merge to Newlist3,newlist3 and List4 merge for Newlist4 ... 2) List1 and List2 merged into List12,list3 and LIST4 merged into List34 ... Then list12 and List34 merge ... Until it is merged into a list.

So what is the difference between these two methods?

Reference blog Http://www.cnblogs.com/TenosDoIt/p/3673188.html learned that the two methods of time complexity is not the same, the key is that the list is a length.

Methods 1:1, 2 merge, Traverse 2n nodes, 12 result and 3 merge, traverse 3n nodes, 123 result and 4 merge, traverse 4n nodes; 123..k-1 results and K merge, traverse KN nodes, total number of nodes traversed is n (2+3+...+k) = N (k^2+k-2)/2, so the time complexity is O (n (k^2+k-2)/2) = O (nk^2).

Method 2: Divide and conquer, the algorithm complexity is t (k) = 2T (K/2) + O (NK), very simple can be deduced to obtain the algorithm complexity of O (NKLOGK).

In fact, personally, the difference between the two methods is that the method 2 each merge two linked list length is relatively close, regardless of the original linked list length is relatively equal, at least compared to the method 1 per merge of two linked list of the length of a small difference.

/** * Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int x) {* val = x; * Next = Null *} *} */public class Solution {public ListNode mergeklists (list<listnode> lists) {if (lists = = nul L | |        Lists.size () < 1) return null;    Return mergelists (lists, 0, Lists.size ()-1);            ListNode mergelists (list<listnode> lists, int from, int to) {if (from < to) {            int mid = (from + to)/2;        Return Merge2lists (mergelists (lists, from, mid), mergelists (lists, Mid + 1, to));    } return Lists.get (from);        } public ListNode merge2lists (ListNode head1, ListNode head2) {if (head1 = = null) return head2;                if (head2 = = null) return head1;        ListNode newhead = head1;        ListNode Node1 = head1, Node2 = head2;            if (Head1.val > Head2.val) {node1 = head2; Node2= Head1;        Newhead = head2;                } while (Node1.next = null && Node2! = null) {if (Node2.val < Node1.next.val) {                ListNode tmp = Node2.next;                Node2.next = Node1.next;                Node1.next = Node2;                Node1 = Node1.next;            Node2 = tmp;            } else {node1 = Node1.next;        }} if (Node2! = null) {Node1.next = Node2;    } return newhead; }    }


"Priority Queue"

Maintain a priority queue, place the head node of all the lists, which is the smallest node, in the queue, remove the smallest element from the queue each time, and then add the nodes that follow the element in the original list to the priority queue until the queue is empty.

The time complexity of adding nodes to the priority queue is O (LGK), which is a total of KN nodes, so the total time complexity is O (KNLGK), as is the division rule.

public class Solution {public ListNode mergeklists (list<listnode> lists) {if (lists = null | | lists.siz                E () < 1) return null; comparator<listnode> cmp = new comparator<listnode> () {public int compare (ListNode node1, ListNode n        Ode2) {return node1.val-node2.val;        }    };                priorityqueue<listnode> queue = new priorityqueue<listnode> (Lists.size (), CMP); for (int i = 0; i < lists.size (); i++) {if (Lists.get (i) = null) {Queue.offer (Lists.get (i)            );        }} ListNode Newhead = new ListNode (0);        ListNode pre = Newhead;            while (!queue.isempty ()) {ListNode cur = queue.poll ();            if (cur.next! = null) {Queue.offer (cur.next);            } pre.next = cur;        Pre = Pre.next;    } return newhead.next; }}


Reference Source: http://blog.csdn.net/linhuanmars/article/details/19899259

"Leetcode" Merge k Sorted Lists Problem Solving report

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.