剑指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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • phpstorm 2021最新激活码破解方法

    phpstorm 2021最新激活码破解方法,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月14日
    58
  • django官方入门教程_DJango

    django官方入门教程_DJangoDjango入门教程Django是一个开放源代码的Web应用框架,由Python写成。采用了MTV的框架模式,即模型M,模板T和视图V。其最大特点自带一个后台管理系统,可以让只要少量代码就能实现后台管理,尤其适合内容管理网站(如博客,新闻,公司首页等信息类网站),适合中小型web网站。Django基本介绍Django安装HelloDjango开发工具

    2025年10月1日
    4
  • java课设_Java 课设

    java课设_Java 课设展开全部简单的五子棋程序新建一个chess类,其中代码如下packagework;importjava.awt.Color;publicclassChess{publicstaticfinalintdiamter=30;privateintx;//在网格中的索引,0~e68a84e8a2ad62616964757a686964616f3133323865663715priva…

    2022年7月12日
    16
  • Struts2框架–学习笔记(上):搭建struts2工程、struts2基本概念、struts2对页面数据的操作

    Struts2框架–学习笔记(上):搭建struts2工程、struts2基本概念、struts2对页面数据的操作

    2021年9月26日
    43
  • Java文件读写

    Java文件读写1.%g用于输出科学计数法2.用printf输出换行除了’\n’,还可以用’%n’3.File类的几个构造方法:1)File(StringdirectoryPath)2)File(StringdirectoryPath,Stringfilename)3)File(FiledirObj,Stringfilename)4)File(URIuri)注意,这里的对象指的可能是一个…

    2022年7月14日
    15
  • 详解C语言中的数组指针与指针数组

    详解C语言中的数组指针与指针数组·详解数组指针与指针数组·数组指针一、区分首先我们需要了解什么是数组指针以及什么是指针数组,如下图:int*p[5];int(*p)[5];数组指针的意思即为通过指针引用数组,p先和*结合,说明了p是一个指针变量,指向一个大小为5的数组。所以,int(*p)[5]即为一个数组指针。int*p[5]则是一个大小为5且存放整型指针的数组。二、数组元素的指针1.定…

    2022年7月27日
    7

发表回复

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

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