Created
November 10, 2018 03:46
-
-
Save zhangxiaomu01/fa1c31c4bdf04cf39c24e6968531f773 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
ListNode* mergeKLists(vector<ListNode*>& lists) { | |
int len_l = lists.size(); | |
ListNode head(0); | |
ListNode *h = &head; | |
while(true){ | |
bool isBreak = true; | |
int minVal = INT_MAX; | |
int min_index = 0; | |
for(int i = 0; i < len_l; i++) | |
{ | |
if(lists[i] != NULL){ | |
isBreak = false; | |
if(lists[i]->val <= minVal) | |
{ | |
min_index = i; | |
minVal = lists[i]->val; | |
} | |
} | |
} | |
if(isBreak) | |
break; | |
h->next = lists[min_index]; | |
h = h->next; | |
lists[min_index] = lists[min_index]->next; | |
} | |
return head.next; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment