面试题25:合并有序链表

面试题25:合并有序链表

题目:合并两个有序(递增)链表,使之融合后仍然有序

分析:设置两个指针分别指向两个链表,通过比较两个链表的值来进行链表的合并
1.常规算法
代码:

//非递归式地融合链表
Node *mergeNodeList(Node *pHead1, Node *pHead2)
{

    if (pHead1 == NULL)
    {
        return pHead2;
    }
    else if (pHead2 == NULL)
    {
        return pHead1;
    }

    Node *pMergeHead = NULL, *pNode = NULL;
    Node *p1 = pHead1, *p2 = pHead2;

    if (p1->value > p2->value)
    {
        pMergeHead = p2;
        pNode = p2;

        p2 = p2->next;
    }
    else
    {
        pMergeHead = p1;
        pNode = p1;
        p1 = p1->next;
    }

    while (p1 != NULL || p2 != NULL)
    {
        if (p1 != NULL && p2 != NULL)
        {
            if (p1->value > p2->value)
            {
                pNode->next = p2;
                pNode = p2;
                p2 = p2->next;
            }
            else
            {
                pNode->next = p1;
                pNode = p1;
                p1 = p1->next;
            }
            continue;
        }

        if (p1 == NULL)
        {
            pNode->next = p2;
            break;
        }

        if (p2 == NULL)
        {
            pNode->next = p1;
            break;
        }
    }

    return pMergeHead;
}
  1. 递归算法:由于每合并一个结点,两个指针还是会指向两个链表剩余的几点的头结点,故可用递归的方式做。
    代码:
//递归式的融合链表
Node *mergeNodeListRecursive(Node *pHead1, Node *pHead2)
{
    if (pHead1 == NULL)
    {
        return pHead2;
    }
    else if (pHead2 == NULL)
    {
        return pHead1;
    }

    Node *pMergeHead = NULL;
    if (pHead1->value > pHead2->value)
    {
        pMergeHead = pHead2;
        pMergeHead->next = mergeNodeListRecursive(pHead1, pHead2->next);
    }
    else
    {
        pMergeHead = pHead1;
        pMergeHead->next = mergeNodeListRecursive(pHead2, pHead1->next);
    }

    return pMergeHead;
}

可执行代码:

#include <iostream>
using namespace std;

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

//新增结点
Node *addNewNode(Node *pHead, int newValue);

//打印链表
void printNodeList(Node *pHead);

//递归式的融合链表
Node *mergeNodeListRecursive(Node *pHead1, Node *pHead2);

//非递归式地融合链表
Node *mergeNodeList(Node *pHead1, Node *pHead2);

int main()
{
    Node *pHead1 = new Node();
    pHead1->value = 0;
    pHead1->next = NULL;
    addNewNode(pHead1, 1);
    addNewNode(pHead1, 3);
    addNewNode(pHead1, 5);
    addNewNode(pHead1, 7);
    addNewNode(pHead1, 9);

    Node *pHead2 = new Node();
    pHead2->value = 0;
    pHead2->next = NULL;
    addNewNode(pHead2, 2);
    addNewNode(pHead2, 4);
    addNewNode(pHead2, 6);
    addNewNode(pHead2, 8);
    addNewNode(pHead2, 10);

    Node *mergeHead = mergeNodeList(pHead1, pHead2);
    printNodeList(mergeHead);
    return 0;
}

//递归式的融合链表
Node *mergeNodeListRecursive(Node *pHead1, Node *pHead2)
{
    if (pHead1 == NULL)
    {
        return pHead2;
    }
    else if (pHead2 == NULL)
    {
        return pHead1;
    }

    Node *pMergeHead = NULL;
    if (pHead1->value > pHead2->value)
    {
        pMergeHead = pHead2;
        pMergeHead->next = mergeNodeListRecursive(pHead1, pHead2->next);
    }
    else
    {
        pMergeHead = pHead1;
        pMergeHead->next = mergeNodeListRecursive(pHead2, pHead1->next);
    }

    return pMergeHead;
}

//非递归式地融合链表
Node *mergeNodeList(Node *pHead1, Node *pHead2)
{

    if (pHead1 == NULL)
    {
        return pHead2;
    }
    else if (pHead2 == NULL)
    {
        return pHead1;
    }

    Node *pMergeHead = NULL, *pNode = NULL;
    Node *p1 = pHead1, *p2 = pHead2;

    if (p1->value > p2->value)
    {
        pMergeHead = p2;
        pNode = p2;

        p2 = p2->next;
    }
    else
    {
        pMergeHead = p1;
        pNode = p1;
        p1 = p1->next;
    }

    while (p1 != NULL || p2 != NULL)
    {
        if (p1 != NULL && p2 != NULL)
        {
            if (p1->value > p2->value)
            {
                pNode->next = p2;
                pNode = p2;
                p2 = p2->next;
            }
            else
            {
                pNode->next = p1;
                pNode = p1;
                p1 = p1->next;
            }
            continue;
        }

        if (p1 == NULL)
        {
            pNode->next = p2;
            break;
        }

        if (p2 == NULL)
        {
            pNode->next = p1;
            break;
        }
    }

    return pMergeHead;
}

//新增结点
Node *addNewNode(Node *pHead, int newValue)
{

    Node *node = pHead;
    while (node->next != NULL)
    {
        node = node->next;
    }

    Node *newNode = new Node();
    newNode->value = newValue;
    newNode->next = NULL;

    node->next = newNode;
    node = NULL;
    return newNode;
}

//打印链表
void printNodeList(Node *pHead)
{

    Node *node = pHead;
    while (node != NULL)
    {
        cout << node->value << ",";
        node = node->next;
    }
    cout << endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值