ListNode* mergeKLists(vector<ListNode*>& lists, int low, int high) { if(high > low) { return mergeTwoLists(mergeKLists(lists, low, (low+high)/2), mergeKLists(lists, (low+high)/2+1, high)); } else { return lists[low]; } }
ListNode *mergeKLists(vector<ListNode *> &lists, int begin, int end) { if(begin > end) return NULL; if(begin == end) return lists[begin]; return mergeTwoLists(mergeKLists(lists, begin, (begin + end) / 2), mergeKLists(lists, (begin + end) / 2 + 1, end)); }
struct ListNode *mergeKLists(struct ListNode **lists, int listsSize) { int mid; struct ListNode *newhead, **headpp; struct ListNode *left, *right; if (!listsSize) return(0); if (listsSize == 1) return(lists[0]); mid = listsSize / 2; left = mergeKLists(lists, mid); right = mergeKLists(lists + mid, listsSize - mid); newhead = 0; headpp = &newhead; while (left && right) { if (left->val < right->val) { *headpp = left; left = left->next; headpp = &(*headpp)->next; } else { *headpp = right; right = right->next; headpp = &(*headpp)->next; } } if (left) *headpp = left; if (right) *headpp = right; return(newhead); }
ListNode* mergeKLists(vector<ListNode*>& lists) { if (lists.size()==0) return NULL; if (lists.size()==1) return lists[0]; if (lists.size()==2) return merge2Lists(lists[0],lists[1]); vector<ListNode*> vec1(lists.begin(),lists.begin()+lists.size()/2); vector<ListNode*> vec2(lists.begin()+lists.size()/2,lists.end()); return merge2Lists(mergeKLists(vec1),mergeKLists(vec2)); }
ListNode * mergeKLists (vector<ListNode *> & lists, int start, int end) { if (start == end) return lists[start]; if (start + 1 == end) return merge(lists[start], lists[end]); int mid = (start + end) / 2; return merge(mergeKLists(lists, start, mid), mergeKLists(lists, mid + 1, end)); }
ListNode* mergeKLists(vector<ListNode*>& lists, int start, int end) { if (start == end) { return lists[start]; } int mid = (start + end) / 2; ListNode* left = mergeKLists(lists, start, mid); ListNode* right = mergeKLists(lists, mid+1, end); return mergeTwoLists(left, right); }
ListNode* mergeKLists(vector<ListNode*>& lists) { if (lists.size() == 0) return NULL; if (lists.size() == 1) return lists[0]; if (lists.size() == 2) return merge(lists[0], lists[1]); vector<ListNode*> left, right; left.insert(left.end(), lists.begin(), lists.begin() + lists.size()/2); right.insert(right.end(), lists.begin() + lists.size()/2, lists.end()); ListNode *newleft = mergeKLists(left); ListNode *newright = mergeKLists(right); ListNode* mylist = merge(newleft, newright); return mylist; }//end margekLists
ListNode *mergeKLists(vector<ListNode *> &lists, int lo, int hi) { if(lo > hi) { return NULL; } if(lo == hi) { return lists[lo]; } int mid = (lo+hi)/2; ListNode *left = mergeKLists(lists, lo, mid); ListNode *right = mergeKLists(lists, mid+1, hi); return merge(left, right); }
ListNode* mergeKLists(vector<ListNode*>& lists) { if (lists.empty()) return NULL; if (lists.size() == 1) return lists[0]; if (lists.size() == 2) return merge(lists[0], lists[1]); vector<ListNode*>part1(lists.begin(), lists.begin() + lists.size() / 2); vector<ListNode*>part2(lists.begin() + lists.size() / 2, lists.end()); ListNode* l1 = mergeKLists(part1); ListNode* l2 = mergeKLists(part2); return merge(l1, l2); }
// ----------------------------------------------------------------------------- //virtual bool Solution::Test() { bool pass = true; { int a1[] = { -1, 1 }; int a2[] = { -3, 1, 4 }; ListNode * l1 = createList(a1, sizeof(a1) / sizeof(int)); ListNode * l2 = createList(a2, sizeof(a2) / sizeof(int)); std::vector<ListNode *> lists; lists.push_back(l1); lists.push_back(l2); ListNode * result = mergeKLists(lists); std::vector<int> vec = transform(result); pass = pass && ((vec[0] == -3) && (vec[1] == -1) && (vec[2] == 1) && (vec[3] == 1) && (vec[4] == 4) ); } { int a1[] = { -1, 1 }; int a2[] = { -3, 1, 4 }; int a3[] = { -2, -1, 0, 2 }; ListNode * l1 = createList(a1, sizeof(a1) / sizeof(int)); ListNode * l2 = createList(a2, sizeof(a2) / sizeof(int)); ListNode * l3 = createList(a3, sizeof(a3) / sizeof(int)); std::vector<ListNode *> lists; lists.push_back(l1); lists.push_back(l2); lists.push_back(l3); ListNode * result = mergeKLists(lists); std::vector<int> vec = transform(result); pass = pass && ((vec[0] == -3) && (vec[1] == -2) && (vec[2] == -1) && (vec[3] == -1) && (vec[4] == 0) && (vec[5] == 1) && (vec[6] == 1) && (vec[7] == 2) && (vec[8] == 4) ); } return pass; }
ListNode* mergeKLists(vector<ListNode*>& lists) { if (lists.size()==0) return NULL; if (lists.size()==1) return lists[0]; int size=lists.size(); int mid=(size)/2; vector<ListNode*> l11 = vector<ListNode*>(lists.begin(), lists.begin()+mid); ListNode* l1=mergeKLists(l11); vector<ListNode*> l22 = vector<ListNode*>(lists.begin()+mid, lists.end()); ListNode* l2=mergeKLists(l22); ListNode* ret = merge2List(l1, l2); return ret; }
struct ListNode *mergeKLists(struct ListNode **lists, int listsSize) { if (listsSize == 0 || lists == NULL) { return NULL; } if (listsSize == 1) { return lists[0]; } if (listsSize == 2) { return mergeTwoLists(lists[0], lists[1]); } struct ListNode *a = mergeKLists(lists, listsSize / 2); struct ListNode *b = mergeKLists(lists + listsSize / 2, listsSize - listsSize / 2); return mergeTwoLists(a, b); }
int main() { struct ListNode** lists = NULL; int listsSize = 2; int i = 0; int len = 2; int a0[] = {1, 3}; int *a1 = NULL; int **a; int count[] = {0, 2}; struct ListNode * answer = NULL; a = (int **) malloc(sizeof(int *) * len); *(a + 0) = a1; *(a + 1) = a0; lists = (struct ListNode **) malloc(sizeof(struct ListNode *) * listsSize); for (i = 0; i < listsSize; i ++) { *(lists + i) = convert_int_to_ListNode(*(a + i), count[i]); } answer = mergeKLists(lists, listsSize); print_ListNode(answer); free_ListNode(answer); free(a); for (i = 0; i < listsSize; i ++) { free(*(lists + i)); } free(lists); printf("end\r\n"); return 0; }
ListNode *mergeKLists(vector<ListNode *> &lists) { if(lists.empty()) { return NULL; } return mergeKLists(lists,0,lists.size()-1); }
ListNode *mergeKLists(vector<ListNode *> &lists,unsigned int s,unsigned int e) { if(s == e) { return lists[s]; } if(s + 1 == e) { return mergeTwoLists(lists[s],lists[e]); } else { ListNode* left = mergeKLists(lists,s,s + (e - s)/2); ListNode* right = mergeKLists(lists,s + (e - s)/2 + 1,e); return mergeTwoLists(left,right); } }
//Divide and conquer, less time is needed but still needs to be optimized ListNode* mergeKLists(vector<ListNode*>& lists) { int n=lists.size(); if(n==0) return NULL; else if(n==1) return lists[0]; int i=0; int mid=(n-1)/2; vector<ListNode*> l; vector<ListNode*> r; for(;i<mid+1;i++) l.push_back(lists[i]); for(;i<n;i++) r.push_back(lists[i]); ListNode* left=mergeKLists(l); ListNode* right=mergeKLists(r); return mergeTwoLists(left,right); }
int main(){ int x[] = {1,3,5}; int y[] = {2,4,6}; ListNode *x1 = getList(x[],sizeof(x)/sizeof(x[0])); ListNode *x2 = getList(y[],sizeof(y)/sizeof(y[0])); vector<ListNode*> lists; lists.push_back(x1); lists.push_back(x2); ListNode* result = mergeKLists(lists); print(result); }
ListNode *mergeKLists(vector<ListNode *> &lists) { int k = (int) lists.size(); if (k == 0){ return NULL; } ListNode **minEle = min_element(&lists[0], &lists[k], comp); if (*minEle == NULL){ return NULL; } ListNode *head = postNext(*minEle); head->next = mergeKLists(lists); return head; }
bool testMergeKLists() { int a[][4] = { {0, 1, 4, 7}, {2, 3, 4, 8}, {1, 4, 7, 9} }; int n = COUNT_OF(a); struct ListNode **lists = malloc(sizeof(struct ListNode *) * n); int i; for (i = 0; i != n; i++) { lists[i] = buildLst(a[i], COUNT_OF(a[i])); } struct ListNode *r = mergeKLists(lists, n); showLst(r); return true; }
ListNode *mergeKLists(vector<ListNode *> &lists) { if(lists.size()==0) return NULL; if(lists.size()>=2) { vector<ListNode *> temp; for(int i=0;i<lists.size()/2;i++) temp.push_back(mergeTwoLists(lists[2*i],lists[2*i+1])); if(lists.size()%2==1) temp.push_back(lists.back()); lists = temp; mergeKLists(lists); } return lists[0]; }
int main(int arg, char *argv[]) { // insert code here... printf("LeetCode 023 Merge k Sorted Lists, C ...\n"); struct ListNode *a1 = 0;//(struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode *a2 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode *a3 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode *b1 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode *b2 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode *b3 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode *c1 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode *c2 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode *c3 = (struct ListNode*)malloc(sizeof(struct ListNode)); //a1->val = 1; a2->val = 4; a3->val = 7; b1->val = 2; b2->val = 5; b3->val = 8; c1->val = 3; c2->val = 6; c3->val = 9; //a1->next = a2; a2->next = a3; b1->next = b2; b2->next = b3; c1->next = c2; c2->next = c3; //printList(a1); //printList(mergeTwoLists(a1, b1)); int n = 3; struct ListNode **lists = (struct ListNode**)malloc(sizeof(struct ListNode*) * n); lists[0] = a1; lists[1] = b1; lists[2] = c1; printVector(lists, n); mergeKLists(lists, n); return 0; }
ListNode* mergeKLists(vector<ListNode*>& lists) { if(lists.size() == 0) { return NULL; } if(lists.size() == 1) { return lists[0]; } vector<ListNode*> res; for(int i=0; i<lists.size(); i+=2) { if(i+1< lists.size()) { res.push_back(merge(lists[i], lists[i+1])); } else { res.push_back(lists[i]); } } return mergeKLists(res); }
int main(int argc, const char *argv[]) { struct ListNode *arr[2], *p, *p1; //p = arr[0]; /* p1 = (struct ListNode * )malloc(sizeof(struct ListNode)); p1->val = 232; p1->next = NULL; arr[0] = p1; p1 = (struct ListNode * )malloc(sizeof(struct ListNode)); p1->val = 39; p1->next = NULL; arr[1] = p1; p1 = (struct ListNode * )malloc(sizeof(struct ListNode)); p1->val = 239; p1->next = NULL; arr[1]->next = p1; */ /* for(int i = 0;i < 2;i++) { p = arr[i]; printf("\n"); while(p != NULL) { //printf("sdfadsf\n"); //printf("%d\n" , p->val); p = p->next; } } */ p = mergeKLists(arr,2); while(p != NULL) { printf("%d\t", p->val); p = p->next; } printf("\n"); return 0; }
/** * @param lists: a list of ListNode * @return: The head of one sorted list. */ ListNode *mergeKLists(vector<ListNode *> &lists) { // write your code here return mergeKLists(lists, 0, lists.size()-1); }
ListNode *mergeKLists(vector<ListNode *> &lists) { return mergeKLists(lists, 0, lists.size() - 1); }
ListNode* mergeKLists(vector<ListNode*>& lists) { int num = lists.size(); if(!num) return NULL; return mergeKLists(lists, 0, num-1); }