Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
題意:合并兩個(gè)有序單鏈表,合并后的仍然是有序的。。。。。。。。。。。。。。。。。。。

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比靜海網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式靜海網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋靜海地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
//首先判斷有沒有空鏈表的情況。。。。。
if(l1 && !l2)
return l1;
if(!l1 && l2)
return l2;
if(!l1 && !l2)
return NULL;
//還是和之前的002題要保存新鏈表頭,中間節(jié)點(diǎn)head負(fù)責(zé)遍歷
struct ListNode* head;
struct ListNode* ret;
//找到新鏈表的頭
if(l1->val<l2->val){
head=l1;
l1=l1->next;
}else{
head=l2;
l2=l2->next;
}
ret=head;
//負(fù)責(zé)遍歷。哪個(gè)小就指向哪個(gè),直到有一個(gè)遍歷完
while(l1&&l2){
if(l1->val<l2->val){
head->next=l1;
l1=l1->next;
}else{
head->next=l2;
l2=l2->next;
}
head=head->next;
}
//遍歷完后看看誰還剩下直接指向剩下的部分
if(l1){
head->next=l1;
}
if(l2){
head->next=l2;
}
return ret;
}。。。。。。。。。。。。。。。。太笨了。。。。。。。。。。。。。。。。繼續(xù)練習(xí)吧少年。。。。。。。。。。。。。。。
網(wǎng)站名稱:LeetCode021MergeTwoSortedListssC語言
網(wǎng)站路徑:http://www.chinadenli.net/article32/iggipc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、外貿(mào)網(wǎng)站建設(shè)、企業(yè)建站、定制開發(fā)、網(wǎng)站內(nèi)鏈、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)