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 *l3 = NULL;//Head Pointer -ListNode *p =L3; - //if (L1 = = NULL && L2 = = null) the //return NULL; - //else if - //return (L1 = = NULL)? l1::l2; - if(L1 = = NULL)returnL2; + if(L2 = = NULL)returnL1; - while(l1&&L2) + { A if(L3 = =NULL) at { - if(L1->val <= l2->val) - { -L3 =L1; -L1 = l1->Next; -L3->next =NULL; inp =L3; - } to Else if(L2->val <= l1->val) + { -l3=L2; theL2 = l2->Next; *L3->next =NULL; $p =L3;Panax Notoginseng } - } the Else + { A if(L1->val <= l2->val) the { +P->next =L1; -p =L1; $L1 = l1->Next; $P->next =NULL; - } - Else if(L2->val <= l1->val) the { -P->next =L2;Wuyip =L2; theL2 = l2->Next; -P->next =NULL; Wu } - } About } $ if(L1) P->next =L1; - if(L2) P->next =L2; - returnL3; - } A};
(Leetcode) Merge Sorted Lists