剑指Offer面试题:13.合并两个排序的链表

一题目:合并两个排序的链表二代码实现将链表换成数组做简单的循环和递归测试(1)循环实现(2)递归实现

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

全栈程序员社区此处内容已经被作者隐藏,请输入验证码查看内容
验证码:
请关注本站微信公众号,回复“验证码”,获取验证码。在微信里搜索“全栈程序员社区”或者“www_javaforall_cn”或者微信扫描右侧二维码都可以关注本站微信公众号。

一 题目:合并两个排序的链表

题目:输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按照递增排序的。例如输入下图中的链表1和链表2,则合并之后的升序链表如链表3所示。


剑指Offer面试题:13.合并两个排序的链表

二 代码实现

template <typename T>
struct Node
{
public:
    T data;
    Node *pNext;
};

template <typename T>
class ListEx
{
private:
    Node<T> *m_pHead;
    Node<T> *m_pTail;
public:
    ListEx()
    {
        m_pTail = m_pHead = NULL;
    }
    ~ListEx()
    {
        Node<T> *pTemp = NULL;
        Node<T> *pNode = m_pHead;
        while (pNode)
        {
            pTemp = pNode;
            pNode = pNode->pNext;
            delete pTemp;
        }

        m_pHead = m_pTail = NULL;
    }
    void add(T data)
    {
        Node<T> *pNode = new Node<T>;
        pNode->data = data;
        pNode->pNext = NULL;

        if (m_pHead == NULL)
        {
            m_pTail = m_pHead = pNode;
        }

        Node<T>* pTemp = m_pTail;
        pTemp->pNext = pNode;
        m_pTail = pNode;
    }

    Node<T> *GetListHead()
    {
        return m_pHead;
    }
};
template <typename T>
Node<T>* RebuildArray(Node<T>* pNode1, Node<T>* pNode2)
{
    if (NULL == pNode1)
    {
        return pNode2;
    }
    else if (NULL == pNode2)
    {
        return pNode1;
    }
    Node<T>* pNewNode = new Node<T>;
    pNewNode = NULL;
    if (pNode1->data <= pNode2->data)
    {
        pNewNode = pNode1;
        pNewNode->pNext = RebuildArray(pNode1->pNext, pNode2);
    }
    else
    {
        pNewNode = pNode2;
        pNewNode->pNext = RebuildArray(pNode1, pNode2->pNext);
    }

    return pNewNode;
}
void main()
{
    ListEx<int> *pList1= new ListEx<int>();
    pList1->add(1);
    pList1->add(3);
    pList1->add(5);
    pList1->add(7);
    Node<int> *pHead1 = pList1->GetListHead();

    ListEx<int> *pList2= new ListEx<int>();
    pList2->add(2);
    pList2->add(4);
    pList2->add(6);
    pList2->add(8);
    Node<int> *pHead2 = pList2->GetListHead();

    Node<int>* p = RebuildArray(pHead1, pHead2);
}

将链表换成数组做简单的循环和递归测试

  (1)循环实现

void RebuildArray(int *a, int nLen1, int *b, int nLen2, int *pNew)
{
    if (NULL == a || NULL == b || 0 == nLen1 || 0 == nLen2 || NULL == pNew)
    {
        return;
    }

    int nIndex = 0;
    int i = 0;
    int j = 0;
    while (i < nLen1)
    {
        while (j < nLen2)
        {
            if (a[i] <= b[j])
            {
                pNew[nIndex++] = a[i++];
                break;
            }
            else
            {
                pNew[nIndex++] = b[j++];
            }
        }    
    }
    while(i < nLen1)
    {
        pNew[nIndex++] = a[i++];
    }
    while(j < nLen2)
    {
        pNew[nIndex++] = b[j++];
    }
}

  (2)递归实现

void RebuildArray_2(int *aStart, int *aEnd, int *bStart, int *bEnd, int *pNew)
{
    if (aStart > aEnd)
    {
        *pNew = *bStart;
        return;
    }
    else if (bStart > bEnd)
    {
        *pNew = *aStart;
        return;
    }
    if (*aStart <= *bStart)
    {
        *pNew = *aStart;
        RebuildArray_2(aStart+1, aEnd, bStart, bEnd, pNew+1);
    }
    else
    {
        *pNew = *bStart;
        RebuildArray_2(aStart, aEnd, bStart+1, bEnd, pNew+1);
    }
}
void RebuildArray_1(int *a, int nLen1, int *b, int nLen2, int *pNew)
{
    if (NULL == a || NULL == b || 0 == nLen1 || 0 == nLen2 || NULL == pNew)
    {
        return;
    }
    
    int *aStart = a;
    int *aEnd = &a[nLen1 - 1];
    int *bStart = b;
    int *bEnd = &b[nLen2 - 1];

    RebuildArray_2(aStart, aEnd, bStart, bEnd, pNew);
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/120160.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Python装饰器

    装饰器表现形式1.函数装饰器编写自定义装饰器有许多方法,但最简单的方法是编写一个函数,返回包装原始函数调用的一个子函数例1:>>>[DEBUG]:entersay_he

    2021年12月18日
    43
  • findIndex()方法[通俗易懂]

    findIndex()方法[通俗易懂]findIndex()方法返回数组中通过测试的第一个元素的索引(作为函数提供)。findIndex()方法对数组中存在的每个元素执行一次函数:如果找到函数返回true值的数组元素,则findIndex()返回该数组元素的索引(并且不检查剩余值)否则返回-1注释:findIndex()不会为没有值的数组元素执行函数。注释:findIndex()不会改变原始数组。获取数组中第一个值等于或大于18的元素的索引:varages=[3,10,18,20];funct

    2022年10月26日
    0
  • 卡尔曼滤波(KF)与扩展卡尔曼滤波(EKF)的一种理解思路及相应推导(1)

    卡尔曼滤波(KF)与扩展卡尔曼滤波(EKF)的一种理解思路及相应推导(1)前言:从上个世纪卡尔曼滤波理论被提出,卡尔曼滤波在控制论与信息论的连接上做出了卓越的贡献。为了得出准确的下一时刻状态真值,我们常常使用卡尔曼滤波、扩展卡尔曼滤波、无迹卡尔曼滤波、粒子滤波等等方法,这些方法在姿态解算、轨迹规划等方面有着很多用途。卡尔曼滤波的本质是参数化的贝叶斯模型,通过对下一时刻系统的初步状态估计(即状态的先验估计)以及测量得出的反馈相结合,最终得到下一时刻较为准确的的状态估计

    2022年6月28日
    46
  • scaleType详解

    scaleType详解scaleType详解

    2022年6月16日
    41
  • oracle触发器类型

    http://www.cnblogs.com/roucheng/p/3506033.html触发器是许多关系数据库系统都提供的一项技术。在ORACLE系统里,触发器类似过程和函数,都有声明,执行和异

    2021年12月23日
    52
  • COM编程之三 QueryInterface

    COM编程之三 QueryInterface【1】IUnknown接口客户同组件交互都是通过接口完成的。在客户查询组件的其它接口时,也是通过接口完成的。而那个接口就是IUnknown。IUnknown接口的定义包含在Win32SDK中的U

    2022年7月4日
    21

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号