class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if (l1 == NULL) return l2; if(l2 == NULL)return l1; ListNode *head = new ListNode(66); ListNode *pp = l1; ListNode *qq = l2; ListNode *end = head; while (pp && qq){ if (pp->val < qq->val){ end = end->next = pp; pp = pp->next; } else{ end = end->next = qq; qq = qq->next; } } if (pp){ end->next = pp; } else{ end->next = qq; } return head->next; } };
思路:创建一个节点,谁小就放谁,肯定会有一个链先结束,则把后面的链直接连上即可。