如何将2个链接列表合并为1?

时间:2014-04-20 15:01:37

标签: list merge linked-list

我试图找到答案,但无法找到明确的表达方式。

我有以下代码:

slist* mergeLists(slist *List1,slist *List2)

{
    slist *temp=*List1;

    while (temp->next!=NULL)
        temp=temp->next;

    temp->next=List2;

    return List1;
}

我尝试将List1与List2合并并返回List 1(列表1->列表2)。 但我不知道如何继续。

谢谢

1 个答案:

答案 0 :(得分:0)

我的书签中的搜索产生了这个link,之前我帮助我实现了两个列表的合并。这基本上是做什么的

Node *nodeC;                           // The combined list
while(nodeA != null && nodeB != null) 
{
    if(nodeA->Id <= nodeB->Id)         // Sorting
    { 
        nodeC->Link = nodeA;           // Set nodeA to the link of the current node
        nodeC = nodeA;                 // Set nodeC to nodeA (which is now the current node)
        nodeA = nodeA->link; 
    }
    else 
    {
        nodeC->Link = nodeB;
        noceC = nodeB; 
        nodeB = nodeB->Link;
    }
}
相关问题