/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode Node;
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
if(l1==NULL)
return l2;
if(l2==NULL)
return l1;
//取小的尾插
Node*head=NULL,*tail=NULL;
if(l1->val<l2->val){
head=tail=l1;
l1=l1->next;
}else{
head=tail=l2;
l2=l2->next;
}
while(l1&&l2){
if(l1->val<l2->val){
tail->next=l1;
l1=l1->next;
}else{
tail->next=l2;
l2=l2->next;
}
tail=tail->next;
}
if(l1)
tail->next=l1;
else
tail->next=l2;
return head;
}
【数据结构】【链表代码】合并有序链表
最新推荐文章于 2024-11-10 22:49:00 发布