/********************************************************************************************************** Merge two sorted linked lists and return it as a new list. e new list should be made by splicing together the nodes of the first two lists. **********************************************************************************************************/
class Solution{ public: ListNode *mergeTwoLists(ListNode*l1 , ListNode *l2){ ListNode head(-1); for(ListNode * p = &head;l1 != nullptr || l2 != nullptr;p = p->next){ int val1 = l1 == nullptr ? INT_MAX : l1->val; int val2 = l2 == nullptr ? INT_MAX : l2->val; if(val1 <= val2){ p->next = l1; l1 = l1->next; }else{ p->next = l2; l2 = l2->next; } } return head.next; } };
参考资料:
LeetCode题解