Question Link - Click
Node *merge(Node *node1, Node *node2)
{
if(!node1) return node2;
if(!node2) return node1;
Node *temp = NULL;
if(node1->data <= node2->data)
{
temp = node1;
temp->next = merge(node1->next, node2);
}
else
{
temp = node2;
temp->next = merge(node1, node2->next);
}
return temp;
}
Node * mergeKLists(Node *arr[], int N)
{
// Your code here
if(N < 1) return NULL;
Node *node1 = arr[0];
for(int i = 1; i < N; i++)
{
Node *node2 = arr[i];
node1 = merge(node1, node2);
}
return node1;
}
0 Comments