第一次独立写出hard难度的题,记录一下,虽然确实水(逃
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
ListNode rtn = new ListNode();
rtn.next = null;
ListNode li = rtn;
ArrayList<Integer> list = new ArrayList();
for(ListNode l : lists){
ListNode tmp = l;
while (tmp != null){
list.add(tmp.val);
tmp = tmp.next;
}
}
Integer[] ans = new Integer[list.size()];
list.toArray(ans);
Arrays.sort(ans);
for (int i = 0; i < ans.length; i++) {
li.next = new ListNode(ans[i]);
li = li.next;
}
return rtn.next;
}
}