/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* merge(ListNode* l1, ListNode* l2) {
ListNode *dummy = new ListNode(0);
ListNode *cur = dummy;
while(l1!=NULL&&l2!=NULL){
if(l1->val<l2->val){
cur->next=l1;
l1=l1->next;
}
else{
cur->next=l2;
l2=l2->next;
}
cur=cur->next;
}
if(l1==NULL){
while(l2){
cur->next=l2;
l2=l2->next;
cur=cur->next;
}
}
else{
while(l1){
cur->next=l1;
l1=l1->next;
cur=cur->next;
}
}
return dummy->next;
}
};
acwing 36. 合并两个排序的链表
最新推荐文章于 2024-06-21 10:46:49 发布