1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
/*Linked list node structure struct node { int data; struct node* next; };*/ /*You are required to complete this function*/ node * mergeKList(node *arr[],int N) { //Your code here node *headArray[11]; node *tmpNodes[11]; node *newHead,*tmp,*t; for(int i=0;i<=N;i++) { tmpNodes[i]=arr[i]; } bool f=1; bool first=0; while(f) { int min=10000000; int lastIdx=-1; f=0; for(int i=0;i<=N;i++) { if(tmpNodes[i]!=NULL) { f=1; if(min>tmpNodes[i]->data) { min=tmpNodes[i]->data; lastIdx=i; } } } if(lastIdx<0) break; if(first==0) { tmp=tmpNodes[lastIdx]; newHead=tmp; first=1; } else { tmp->next=tmpNodes[lastIdx]; tmp=tmp->next; } tmpNodes[lastIdx]=tmpNodes[lastIdx]->next; } return newHead; } |