/* * 23. Merge k Sorted Lists * 2016-4-16 By Mingyang * 正如merge 2 list里面说的,可以利用那个来进行简化 */ public ListNode mergeKLists(ListNode[] lists) { if (lists.length == 0) { return null; } return mergeHelper(lists, 0, lists.length - 1); } private ListNode mergeHelper(ListNode[] lists, int start, int end) { if (start == end) { return lists[start]; } int mid = start + (end - start) / 2; ListNode left = mergeHelper(lists, start, mid); ListNode right = mergeHelper(lists, mid + 1, end); return mergeTwoLists(left, right); // 运用到我们前面所说的merge two lists的方法 } public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if(l1==null) return l2; if(l2==null) return l1; if(l1.val<l2.val){ l1.next=mergeTwoLists(l1.next,l2); return l1; }else{ l2.next=mergeTwoLists(l1,l2.next); return l2; } }