# [Leetcode] 23. Merging K sorted lists

Source: Internet
Author: User

23. Merging K sort lists

This question is to examine the Code Foundation, Basic skills, the understanding of variables and references.

Not much to say, the idea is basically the same as the 21st question, but changed from two to many

``Class Solution {public ListNode mergeklists (listnode[] lists) {list<listnode> listnodes = new arraylist&        Lt;> (arrays.aslist (lists));        ListNode ans = new ListNode (integer.max_value);        ListNode k = ans;            while (Not_finish (listnodes)) {int p = choose (Listnodes);            ListNode pnode = Listnodes.get (p);            K.next = Pnode;            Pnode = Pnode.next;            Listnodes.remove (P);            Listnodes.add (Pnode);        K = K.next;    } return ans.next;        } private int Choose (list<listnode> lists) {int min = Integer.max_value;        int p =-1; for (int i = 0; i < lists.size (); i++) {if (min >= lists.get (i). val) {min = Lists.get (i)                . val;            p = i;    }} return p;            Private Boolean Not_finish (List<listnode> lists) {for (int i = Lists.size ()-1; I >= 0; i--) { if (lists.get (i) = = null) {Lists.remove (i);    }} return Lists.size () > 0; }}``

[Leetcode] 23. Merging K sort lists

Related Keywords:

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.