面試題17 合併兩個排序的鍊錶

2021-07-13 03:52:02 字數 468 閱讀 6334

題目:輸入兩個遞增排序的鍊錶,合併這兩個鍊錶使新鍊錶仍然是遞增排序的。

題目還算簡單,就是要考慮的特殊情況挺多的。**如下:

struct listnode 

};class solution

else

current=current->next;

}current->next=null;

listnode *newhead=newpre->next;

newpre->next=null;

delete newpre;

return newhead;

}};

看了下作者在書中寫的示例**,採用遞迴的方式實現,方便簡潔。**如下:

class solution 

else

return newhead;

}};

面試題17 合併兩個排序的鍊錶

題目描述 輸入兩個單調遞增的鍊錶,輸出兩個鍊錶合成後的鍊錶,當然我們需要合成後的鍊錶滿足單調不減規則。hint 請務必使用鍊錶。輸入 輸入可能包含多個測試樣例,輸入以eof結束。對於每個測試案例,輸入的第一行為兩個整數n和m 0 n 1000,0 m 1000 n代表將要輸入的第乙個鍊錶的元素的個數...

面試題17 合併兩個排序的鍊錶

題目 輸入兩個遞增排序的鍊錶,合併這兩個鍊錶並使新鍊錶中的結點仍然是按照遞增排序的。非遞迴 第一種方法 非遞迴 struct listnode listnode mergetwolist listnode plistonehead,listnode plisttwohead 如果第二個鍊錶為空,則第...

面試題17 合併兩個排序的鍊錶

struct listnode 面試題17 合併兩個排序的鍊錶 include stdafx.h include list.h include include listnode merge listnode phead1,listnode phead2 else return pmergedhead...