I want to merge 2 linked list into 1.
Is the code below ok? Can you Check please?
LIST Merge_List(LIST P,LIST Q)
{
HEADER h = P;
NODEPTR ptr2 =Q;
NODEPTR ptr1 = P;
NODEPTR prev=P;
NODEPTR temp=NULL;
/* Second List Pointer */
while(ptr2 != NULL)
{
/* First List Pointer */
while(ptr1 != NULL)
{
while(ptr2->Age>ptr1->Age)
{
prev = ptr1;
ptr1 = ptr1->Next;
}
if(prev == ptr1)
{
// This means the first node of the header needs to be changed
temp = New_Node();
temp->Age = ptr2->Age;
temp->Next = prev;
prev = temp;
h = temp;
}
else
{
temp = New_Node();
temp->Age = ptr2->Age;
temp->Next = prev->Next;
prev->Next = temp;
}
break;
}
ptr2 = ptr2->Next;
}
return h;
}