https://leetcode.com/problems/merge-two-sorted-lists/
1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7 * };8 */9 classSolution {Ten Public: Onelistnode* mergetwolists (listnode* L1, listnode*L2) { AListNode *temp; -ListNode * tail=NULL; -ListNode * res=NULL; theListNode * head1=L1; -ListNode * head2=L2; - while(head1!=null&&head2!=NULL) - { + if(head1->valval) - { +temp=Head1; AHead1=head1->Next; at } - Else - { -temp=head2; -Head2=head2->Next; - } in if(res==NULL) - { tores=temp; + } - if(tail!=NULL) thetail->next=temp; *Tail=temp; $ }Panax Notoginseng while(head1!=NULL) - { thetemp=Head1; +Head1=head1->Next; A if(res==NULL) the { +res=temp; - } $ if(tail!=NULL) $tail->next=temp; -Tail=temp; - } the while(head2!=NULL) - {Wuyitemp=head2; theHead2=head2->Next; - if(res==NULL) Wu { -res=temp; About } $ if(tail!=NULL) -tail->next=temp; -Tail=temp; - } A returnRes; + } the};
Merge Sorted Lists