列表

详情


BM4. 合并两个排序的链表

描述

输入两个递增的链表,单个链表的长度为n,合并这两个链表并使新链表中的节点仍然是递增排序的。
数据范围:
要求:空间复杂度 ,时间复杂度

如输入{1,3,5},{2,4,6}时,合并后的链表为{1,2,3,4,5,6},所以对应的输出为{1,2,3,4,5,6},转换过程如下图所示:


或输入{-1,2,4},{1,3,4}时,合并后的链表为{-1,1,2,3,4,4},所以对应的输出为{-1,1,2,3,4,4},转换过程如下图所示:

示例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;
    }
    
};

上一题