2014-04-20 35 views
0

我試圖找到一個答案,但無法找到明確的消除。如何將2個鏈表合併爲1?

我有以下代碼:

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

{ 
    slist *temp=*List1; 

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

    temp->next=List2; 

    return List1; 
} 

我嘗試使用列表2合併List1中並返回列表1(列表1->列表2)。 但我不知道如何繼續。

謝謝

+0

他們排序? –

回答

0

一個搜索我的書籤得到這個link,這在以前幫我achive兩個列表的合併。這基本上是做什麼的

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; 
    } 
} 
+0

考慮添加一些真實的答案。 –

+0

對不起,我忘記了一些人不知道如何使用鏈接;) –

+0

沒有冒犯,但說「在某個地方搜索這個」並不anasnswer –