BM4. 合并两个排序的链表
描述
示例1
输入:
{1,3,5},{2,4,6}
输出:
{1,2,3,4,5,6}
示例2
输入:
{},{}
输出:
{}
示例3
输入:
{-1,2,4},{1,3,4}
输出:
{-1,1,2,3,4,4}
C 解法, 执行用时: 2ms, 内存消耗: 512KB, 提交时间: 2021-10-14
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) { // write code here struct ListNode* res=(struct ListNode*)malloc(sizeof(struct ListNode )); struct ListNode* dumy=res; struct ListNode* l1=pHead1; struct ListNode* l2=pHead2; while(l1!=NULL&&l2!=NULL) { struct ListNode* node=(struct ListNode*)malloc(sizeof(struct ListNode )); if(l1->val<l2->val) { node->val=l1->val; l1=l1->next; } else { node->val=l2->val; l2=l2->next; } res->next=node; res=res->next; } res->next=l1==NULL?l2:l1; return dumy->next; }
C++ 解法, 执行用时: 2ms, 内存消耗: 524KB, 提交时间: 2021-10-23
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { ListNode* pHead=new ListNode(-1); ListNode* cur=pHead; while(pHead1&&pHead2){ if(pHead1->val<=pHead2->val){ cur->next=pHead1; pHead1=pHead1->next; } else{ cur->next=pHead2; pHead2=pHead2->next; } cur=cur->next; } cur->next=pHead1?pHead1:pHead2; return pHead->next; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 524KB, 提交时间: 2021-10-08
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { ListNode* head=new ListNode(-1); ListNode* cur=head; if(!pHead1) return pHead2; if(!pHead2) return pHead1; while(pHead1&&pHead2){ if(pHead1->val<=pHead2->val){ cur->next=pHead1; pHead1=pHead1->next; } else{ cur->next=pHead2; pHead2=pHead2->next; } cur=cur->next; } if(pHead1) cur->next=pHead1; if(pHead2) cur->next=pHead2; return head->next; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 524KB, 提交时间: 2021-10-04
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { ListNode* tmp = new ListNode(0); ListNode* res = tmp; while(pHead1 != nullptr || pHead2 != nullptr) { if(!pHead1) { tmp->next = pHead2; pHead2 = pHead2->next; } else if(!pHead2) { tmp->next = pHead1; pHead1 = pHead1->next; } else { if(pHead1->val < pHead2->val){ tmp->next = pHead1; pHead1 = pHead1->next; } else { tmp->next = pHead2; pHead2 = pHead2->next; } } tmp = tmp->next; } return res->next; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 524KB, 提交时间: 2021-10-04
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { ListNode* list1_last = nullptr,* list1_now = pHead1,* list2_now = pHead2,*List_re = pHead2,*List_temp = nullptr; if(list1_now == nullptr) return list2_now; if(list2_now == nullptr) return list1_now; if(list1_now->val <= list2_now->val) List_re = pHead1; while(list1_now && list2_now) { if(list1_now->val > list2_now->val) { if(list1_last != nullptr) { list1_last->next = list2_now; list1_last = list1_last->next; } List_temp = list2_now->next; list2_now->next = list1_now; list2_now = List_temp; } else { list1_last = list1_now; list1_now = list1_now->next; } } if(list1_now == nullptr) list1_last->next = list2_now; return List_re; } };