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