20140720 链表反转 、合并、二叉树镜像
2014-07-20 14:22
791 查看
1、链表的反转
2、合并链表
3、两颗二叉树,查找是否存在子结构
4、写代码之前先讲思路,举例子和画图是很好的方法-田超(微软)
5、无法解析的外部符号原因是没有包含相应的.lib文件
6、二叉树的镜像
#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文件
6、二叉树的镜像
相关文章推荐
- 二叉树的镜像 (剑指offer)!!!(两个有序链表的合并,链表的逆置)
- 输入一个二叉树,输出其镜像(二叉树反转)
- python_lintcode_93. 平衡二叉树_165. 合并两个排序链表_453. 将二叉树拆成链表
- 单链表反转 - 有序链表合并 - 子树包含
- 二叉树的镜像(反转二叉树)
- 单链表反转 合并 等例子
- 链表:反转、合并
- C4top-玩转二叉树(根据前序中序镜像反转后层次遍历)
- 《剑指offer》学习之--反转链表与合并两个排序链表
- 近期面试题整理(二叉树的中序遍历、合并排序链表数组)
- 链表相关面试题目 反转 合并
- 【练习】单链表反转,合并
- 反转链表后输出、合并两个排序链表、树的子结构 -- 漫漫算法路 刷题篇
- 剑指offer 重建二叉树 旋转数组的最小数字 链表中倒数第k个结点 反转链表
- 二叉树的镜像(反转)
- 【剑指offer】第二十四题(反转链表) 和 第二十五题(合并两个有序链表)
- 链表常用操作 (定义,创建,反转,合并,交叉)
- 反转单链表+合并有序单链表+查找单链表中倒数第k个节点--20150924
- 剑指offer面试题17,18:反转链表+合并有序链表
- 剑指Offer:反转链表、合并两个排序的链表