Merge two sorted linked lists

  Question : Input two linked lists in ascending order, merge the two linked lists and make the nodes in the new linked list still in ascending order.
  Example: List 1: 1 -> 3 -> 5 -> 7 List 2: 2 -> 4 -> 6 -> 8 After merging: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 

  Idea : first determine whether the two linked lists are empty, and if so, return the other linked list directly. If none of them are empty, start the comparison from the first node of the two linked lists, take out the smaller one, and then move to the next node to continue the recursive operation.

  Test cases :
  1. Functional test
  2. Special test: NULL, a node

#include<cstdio>
#include<iostream>
using namespace std;

struct ListNode
{
    int             m_nValue;
    ListNode*       m_pNext;
};

//创建结点
ListNode* CreateListNode(int value)
{
    ListNode* pNode = new ListNode();
    pNode->m_nValue = value;
    pNode->m_pNext = NULL;

    return pNode;
}

//连接结点
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
    if (pCurrent == NULL)
    {
        cout << "连接错误" << endl;
        return;
    }

    pCurrent->m_pNext = pNext;

}

//打印链表
void PrintList(ListNode* pHead)
{
    ListNode* pNode = pHead;
    while (pNode != NULL)
    {
        cout << pNode->m_nValue << " ";
        pNode = pNode->m_pNext;
    }

    cout << endl;
}

//销毁链表
void DestroyList(ListNode* pHead)
{
    ListNode* pNode = pHead;
    while (pNode != NULL)
    {
        pHead = pHead->m_pNext;
        delete pNode;
        pNode = pHead;;
    }
}

ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
    if (pHead1 == NULL)
    {
        return pHead2;
    }
    else if (pHead2 == NULL)
    {
        return pHead1;
    }

    ListNode* pMergedHead = NULL;

    if (pHead1->m_nValue < pHead2->m_nValue)
    {
        pMergedHead = pHead1;
        pMergedHead->m_pNext = Merge(pHead1->m_pNext, pHead2);
    }
    else
    {
        pMergedHead = pHead2;
        pMergedHead->m_pNext = Merge(pHead1, pHead2->m_pNext);
    }

    return pMergedHead;
}


void Test1()
{
    ListNode* node1 = CreateListNode(1);
    ListNode* node2 = CreateListNode(3);
    ListNode* node3 = CreateListNode(5);
    ListNode* node4 = CreateListNode(7);
    ListNode* node5 = CreateListNode(9);

    ListNode* node6 = CreateListNode(2);
    ListNode* node7 = CreateListNode(4);
    ListNode* node8 = CreateListNode(6);
    ListNode* node9 = CreateListNode(8);
    ListNode* node0 = CreateListNode(10);

    ConnectListNodes(node1, node2);
    ConnectListNodes(node2, node3);
    ConnectListNodes(node3, node4);
    ConnectListNodes(node4, node5);

    ConnectListNodes(node6, node7);
    ConnectListNodes(node7, node8);
    ConnectListNodes(node8, node9);
    ConnectListNodes(node9, node0);

    PrintList(node1);

    PrintList(node6);

    ListNode* pHead = Merge(node1, node6);
    PrintList(pHead);

    DestroyList(pHead);

}

void Test2()
{
    ListNode* node1 = CreateListNode(1);
    ListNode* node2 = CreateListNode(3);
    ListNode* node3 = CreateListNode(5);
    ListNode* node4 = CreateListNode(7);
    ListNode* node5 = CreateListNode(9);

    PrintList(node1);

    ListNode* pHead = Merge(node1, NULL);
    PrintList(pHead);

    DestroyList(pHead);
}

void Test3()
{

    ListNode* pHead = Merge(NULL, NULL);

    DestroyList(pHead);


}

int main()
{

    Test1();
    Test2();
    Test3();

    return 0;

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324405710&siteId=291194637