暴力解法
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int comp(const void* p1,const void* p2){
return (*(int*) p1- *(int*) p2);
}
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize){
if (listsSize==0) return NULL;
if (listsSize==1) return lists[0];
struct ListNode* current=NULL;
int*arr=(int* ) malloc(sizeof(int)*1);
int count=0,j=0;
for(int i=0;i<listsSize;i++){
current=lists[i];
while(current!=NULL){
count++;
arr=(int*) realloc(arr,sizeof(int)* count);
arr[j++]=current->val;
current=current->next;
}
}
struct ListNode* new_list=(struct ListNode*) malloc(sizeof(struct ListNode));
struct ListNode* temp=new_list;
if(j==0) return NULL;
qsort(arr,j,sizeof(int),comp);
for(int i=0;i<j;i++){
new_list->val=arr[i];
if(i==j-1){
new_list->val=arr[i];
new_list->next=NULL;
break;
}
new_list->next=(struct ListNode*) malloc(sizeof(struct ListNode));
new_list=new_list->next;
}
new_list->next=NULL;
new_list=temp;
return new_list;
}