合并两个排好序的单链表

示例

思路一:两个链表同时逐个遍历

参考代码

ListNode* combinList(ListNode *head_1, ListNode *head_2)
{
    ListNode *head_3 = NULL;
    if(head_1 == NULL)
    {
        head_3 = head_2;
    }
    else if(head_2 == NULL)
    {
        head_3 = head_2;
    }
    else
    {
        ListNode *p1 = head_1;
        ListNode *p2 = head_2;
        ListNode *pre = NULL;
        if(p1->value < p2->value)
        {
            head_3 = p1;
            pre = head_3;
            p1 = p1->next;
        }
        else
        {
            head_3 = p2;
            pre = head_3;
            p2 = p2->next;
        }
        while(p1 && p2)
        {
            if(p1->value < p2->value)
            {
                pre->next = p1;
                pre = p1;
                p1 = p1->next;
            }
            else
            {
                pre->next = p2;
                pre = p2;
                p2 = p2->next;
            }
        }
        if(p1)
            pre->next = p1;
        if(p2)
            pre->next = p2;
    }
    return head_3;
}

思路二:递归

参考代码

ListNode* combinListRecursion(ListNode *head_1, ListNode *head_2)
{
    if(head_1 == NULL)
        return head_2;
    else if(head_2 == NULL)
        return head_1;
    ListNode *head = NULL;
    if(head_1->value < head_2->value)
    {
        head = head_1;
        head->next = combinListRecursion(head_1->next, head_2);
    }
    else
    {
        head = head_2;
        head->next = combinListRecursion(head_1, head_2->next);
    }
    return head;
}

执行

 

#include <iostream>
using namespace std;

typedef struct ListNode
{
    int value;
    ListNode* next;
}ListNode;

void createList_1(ListNode *&head)
{
    head = new(ListNode);
    head->value = 1;
    head->next = NULL;

    ListNode *p2 = new(ListNode);
    p2->value = 3;
    p2->next = NULL;
    head->next = p2;

    ListNode *p3 = new(ListNode);
    p3->value = 5;
    p3->next = NULL;
    p2->next = p3;

    ListNode *p4 = new(ListNode);
    p4->value = 7;
    p4->next = NULL;
    p3->next = p4;
}
void createList_2(ListNode *&head)
{
    head = new(ListNode);
    head->value = 2;
    head->next = NULL;

    ListNode *p2 = new(ListNode);
    p2->value = 4;
    p2->next = NULL;
    head->next = p2;

    ListNode *p3 = new(ListNode);
    p3->value = 6;
    p3->next = NULL;
    p2->next = p3;

}
void deleteList(ListNode *p)
{
    ListNode *next = NULL;
    while(p != NULL)
    {
        next = p->next;
        delete p;
        p = NULL;
        p = next;
    }
}



bool deleteKNode_2(ListNode *head, int k)
{
    if(head == NULL || k <= 0)
        return false;
    ListNode *pre = head;
    for(int i = 0; i < k - 1; ++i)
    {
        if(pre == NULL)
            return false;
        pre = pre->next;
    }
    ListNode *cur = head;
    while(pre->next)
    {
        pre = pre->next;
        cur = cur->next;
    }
    cout << cur->value;
    cout << "succeed:" << endl;
    return true;
}

void combinList(ListNode *&head_3, ListNode *head_1, ListNode *head_2)
{
    if(head_1 == NULL)
        head_3 = head_2;
    else if(head_2 == NULL)
        head_3 = head_1;
    else
    {
        ListNode *cur1 = head_1, *cur2 = head_2, *pre = head_3;
        if(cur1->value < cur2->value)
        {
            head_3 = cur1;
            pre = head_3; 
            cur1 = cur1->next;
        }
        else
        {
            head_3 = cur2;
            pre = head_3;
            cur2 = cur2->next;
        }
        while(cur1 && cur2)
        {
            if(cur1->value < cur2->value)
            {
                pre->next = cur1;
                pre = cur1;
                cur1 = cur1->next;
            }
            else
            {
                pre->next = cur2;
                pre = cur2;
                cur2 = cur2->next;
            }
        }
        if(cur1 != NULL)
        {
            pre->next = cur1;
        }
        if(cur2 != NULL)
        {
            pre->next = cur2;
        }
    }
}

void printList(ListNode *p)
{
    while(p)
    {
        cout << p->value << "\t";
        p = p->next;
    }
    cout << endl;
}

int main()
{
    ListNode *head_1 = NULL;
    ListNode *head_2 = NULL;
    ListNode *head_3 = NULL;
    createList_1(head_1);
    createList_2(head_2);
    combinList(head_3, head_1, head_2);
    printList(head_3);

    deleteList(head_3);
}
View Code

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值