// 合併兩個已序單鏈表ide
PSListNode MergeList(PSListNode pL1, PSListNode pL2)spa
{it
PSListNode pNewHead = NULL ;class
PSListNode pL1Node = pL1 ;List
PSListNode pL2N0de = pL2 ;im
PSListNode pNode = NULL ;鏈表
PSListNode pTailNode = NULL ;di
if (pL1 == NULL)view
{vi
return pL2 ;
}
if (pL2 == NULL)
{
return pL1 ;
}
if (pL1Node->data > pL2N0de->data)
{
pNode = pL2N0de;
pL2N0de = pL2N0de->pNext;
}
else
{
pNode = pL1Node;
pL1Node = pL1Node->pNext;
}
pNewHead = pNode;
pTailNode = pNode;
while (pL1Node&&pL2N0de)
{
if (pL1Node->data > pL2N0de->data)
{
pNode = pL2N0de;
pL2N0de = pL2N0de->pNext;
}
else
{
pNode = pL1Node;
pL1Node = pL1Node->pNext;
}
pTailNode->pNext = pNode;
pTailNode = pTailNode->pNext;
}
if (pL1Node == NULL )
{
pTailNode->pNext = pL2N0de;
}
else
{
pTailNode->pNext = pL1Node;
}
return pNewHead;
}
void Print(PSListNode pHead)
{
PSListNode pCurNode = pHead ;
while (pCurNode)
{
printf( "%d->", pCurNode->data);
pCurNode = pCurNode->pNext;
}
printf( "NULL\n");
}