题解 | #合并两个排序的链表#
合并两个排序的链表
https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337
class Solution { public: ListNode* Merge(ListNode* phead1, ListNode* phead2) { if(phead1==NULL)return phead2; if(phead2==NULL)return phead1; ListNode* res=new ListNode(0); auto a=res; ListNode* min; while(1) { a->next=phead1->val>=phead2->val?phead2:phead1; a=a->next; phead1->val>=phead2->val?(phead2=phead2->next):(phead1=phead1->next); if(phead1==NULL||phead2==NULL)break; } if(phead1==NULL) { a->next=phead2; } else { a->next=phead1; } return res->next; } };