/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
struct ListNode* temp;
struct ListNode* l3;
if(l1==NULL&&l2==NULL)
return NULL;
if(l1==NULL&&l2!=NULL) return l2;
if(l1!=NULL&&l2==NULL) return l1;
l3=l1->val<=l2->val? l1:l2;
temp=l3;
if(l1->val<=l2->val) l1=l1->next;
else l2=l2->next;
while(l1&&l2)
{
if(l1->val<=l2->val)
{l3->next=l1;l3=l1;l1=l1->next;}
else {l3->next=l2;l3=l2;l2=l2->next;}
}
l3->next=l1?l1:l2;
return temp;
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
// ListNode l3(0); //设置临时头节点temp_head
ListNode *ptr = new ListNode (0); //使用pre指针指向temp_head
ListNode* temp=ptr;
while(l1&&l2){
if(l1->val<=l2->val)
{
ptr->next=l1;ptr=l1;l1=l1->next;
}
else {ptr->next=l2;ptr=l2;l2=l2->next;}
}
ptr->next=l1? l1:l2;
return temp->next;
}
};
题目链接https://leetcode-cn.com/problems/merge-two-sorted-lists/description/