20140720 链表反转 、合并、二叉树镜像

本文介绍了链表的基本操作,包括链表的反转和两个有序链表的合并,并探讨了如何检查两棵二叉树之间是否存在子结构关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、链表的反转

#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct ListNode
{
    int data;
    struct ListNode * Next;
}ListNode;

ListNode *ReverseList(ListNode *pHead)
{
    ListNode *PReverseHead=NULL;
    ListNode *pBefore=NULL;
    ListNode *pAfter=NULL;
    ListNode *pNode=pHead;
    while(pNode!=NULL)
    {
        pAfter=pNode->Next;

        if(pNode->Next==NULL)
        {PReverseHead=pNode;}
        pNode->Next=pBefore;
        pBefore=pNode;
        pNode=pAfter;
    }
    return PReverseHead;
}

ListNode *CreateList()
{
    int x=0;
    cout<<"input data:";  cin>>x;
    if(x==0)
        return NULL;
    ListNode *pHead=(ListNode *)malloc(sizeof(ListNode));
    pHead->data=x;
    pHead->Next=NULL;
    ListNode *rear=pHead;
    cout<<"input data:";    cin>>x;
    while(x!=0)
    {
        ListNode *p=(ListNode *)malloc(sizeof(ListNode));
        p->data=x;
        p->Next=NULL;
        rear->Next=p;
        rear=p;
        cout<<"input data:";
        cin>>x;
    }
    return pHead;
}

void PrintList(ListNode *pHead)
{
    ListNode *p=pHead;
    while(p!=NULL)
    {
        cout<<p->data<<" ";
        p=p->Next;
    }
}
void main()
{
    ListNode *pHead=CreateList();
    PrintList(pHead);
    ListNode *pReverseHead=ReverseList(pHead);

    PrintList(pReverseHead);
}

2、合并链表

#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct ListNode
{
    int data;
    struct ListNode * Next;
}ListNode;

ListNode *MergeList(ListNode *pHead1,ListNode *pHead2)
{
    if(pHead2==NULL)
        return pHead1;
    if(pHead1==NULL)
        return pHead2;
    ListNode *pMergeHead=NULL;
    if(pHead1->data<pHead2->data)
    {
        pMergeHead=pHead1;
        pHead1->Next=MergeList(pHead1->Next,pHead2);
    }
    else 
    {
        pMergeHead=pHead2;
        pHead2->Next=MergeList(pHead1,pHead2->Next);
    }
    return pMergeHead;
}

ListNode *CreateList()
{
    int x=0;
    cout<<"input data:";  cin>>x;
    if(x==0)
        return NULL;
    ListNode *pHead=(ListNode *)malloc(sizeof(ListNode));
    pHead->data=x;
    pHead->Next=NULL;
    ListNode *rear=pHead;
    cout<<"input data:";    cin>>x;
    while(x!=0)
    {
        ListNode *p=(ListNode *)malloc(sizeof(ListNode));
        p->data=x;
        p->Next=NULL;
        rear->Next=p;
        rear=p;
        cout<<"input data:";
        cin>>x;
    }
    return pHead;
}

void PrintList(ListNode *pHead)
{
    ListNode *p=pHead;
    while(p!=NULL)
    {
        cout<<p->data<<" ";
        p=p->Next;
    }
}
void main()
{
    ListNode *pHead1=CreateList();
    ListNode *pHead2=CreateList();
    ListNode *pMergeNode=MergeList(pHead1,pHead2);
    PrintList(pMergeNode);
}

3、两颗二叉树,查找是否存在子结构

4、写代码之前先讲思路,举例子和画图是很好的方法-田超(微软)

5、无法解析的外部符号原因是没有包含相应的.lib文件

image

6、二叉树的镜像

转载于:https://www.cnblogs.com/yexuannan/p/3856189.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值