利用哑节点+三个指针:
// // Created by jt on 2020/9/26. // class Solution { public: /** * * @param l1 ListNode类 * @param l2 ListNode类 * @return ListNode类 */ ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { // write code here ListNode dummy(0); ListNode *p = &dummy; while (l1 && l2) { if (l1->val > l2->val) { p->next = l2; l2 = l2->next; } else { p->next = l1; l1 = l1->next; } p = p->next; } while (l1) { p->next = l1; l1 = l1->next; p = p->next; } while (l2) { p->next = l2; l2 = l2->next; p = p->next; } p->next = nullptr; return dummy.next; } };