合并有序链表
合并有序链表
http://www.nowcoder.com/questionTerminal/a479a3f0c4554867b35356e0d57cf03d
利用哑节点+三个指针:
// // 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; } };
刷遍天下无敌手 文章被收录于专栏
秋招刷题历程