常见链表操作之链表合并

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Nash_Cyk/article/details/79083101

利用递归思想,拼接两个链表!

//链表合并 递归方式
LIST_NODE * MergeList(LIST_NODE *m_pHead1,LIST_NODE *m_pHead2)
{
    LIST_NODE *MergeListHead  = NULL;
    if (m_pHead1 == NULL)
    {
        return m_pHead2;
    }
    if (m_pHead2 == NULL)
    {
        return m_pHead1;
    }

    if (m_pHead1->num < m_pHead2->num)
    {
        MergeListHead = m_pHead1;
        MergeListHead->next = MergeList(m_pHead1->next,m_pHead2);
    }
    else
    {
        MergeListHead = m_pHead2;
        MergeListHead->next = MergeList(m_pHead1,m_pHead2->next);
    }
    return MergeListHead;
}

猜你喜欢

转载自blog.csdn.net/Nash_Cyk/article/details/79083101