递归合并两链表:LinkList:Merge Two Lists using recursive method
来源:互联网 发布:知乎4.3 编辑:程序博客网 时间:2024/04/29 13:49
// LinkList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
using namespace std;
typedef struct _node
{
int val;
struct _node* next;
}Node;
Node* MergeTwo(Node* h1,Node* h2)
{
Node *h,*a,*b;
h=new Node();
Node* pre=h;
a=h1;
b=h2;
if(a&&b)
{
if(a->val>b->val)
{
pre->next=a;
pre=pre->next;
pre->next=MergeTwo(a->next,b);
}
else
{
pre->next=b;
pre=pre->next;
pre->next=MergeTwo(a,b->next);
}
}
if(!a)
pre->next=b;
if(!b)
pre->next=a;
return h->next;
}
int _tmain(int argc, _TCHAR* argv[])
{
int m=4;
Node* head=(Node*)malloc(sizeof(Node));
head->val=0;
head->next=NULL;
for(int i=1;i<=4;i++)
{
Node* n=(Node*)malloc(sizeof(Node));
n->val=i;
n->next=head;
head=n;
}
Node* head2=new Node();
head2->val=10;
head2->next=NULL;
Node* p2=head2;
for(int i=9;i>4;i--)
{
Node* n=(Node*)malloc(sizeof(Node));
n->val=i;
n->next=NULL;
p2->next=n;
p2=p2->next;
}
Node *tmp=MergeTwo(head,head2);
while(tmp)
{
cout<<tmp->val<<" ";
tmp=tmp->next;
}
cin.get();
return 0;
}
- 递归合并两链表:LinkList:Merge Two Lists using recursive method
- 合并两链表:LinkList:Merge Two LinkList using the general method
- Merge Two Sorted Lists 合并链表
- Merge Two Sorted Lists 合并排序列表
- 【LeetCode21】【Merge Two Sorted Lists】递归实现
- Recursive method(递归方法)
- merge sort using sentinel (recursive)
- 两个有序链表的合并 Merge Two Sorted Lists
- Merge Two Sorted Lists 合并两个有序链表@LeetCode
- [LeetCode]Merge Two Sorted Lists-合并两个有序链表
- Leetcode:Merge Two Sorted Lists 合并两个有序单链表
- Merge Two Sorted Lists 合并两个有序链表
- LeetCode | Merge Two Sorted Lists(合并两个链表)
- Leetcode Merge Two Sorted Lists 合并两个有序链表
- leetcode 21--Merge Two Sorted Lists 链表 合并
- LeetCode 21. Merge Two Sorted Lists(合并链表)
- Merge Two Sorted Lists, 合并两个有序链表
- (LeetCode)Merge Two Sorted Lists --- 合并两个有序序列
- 转载--c++中构造函数初始化的方法以及主要区别
- asp.NET 如何生成DLL文件,并调用
- 转换函数
- CIC(电路识别码)、SLC(信令链路编码)、SLS(信令链路选择码)
- td自动换行(连续的英文字母和数字)
- 递归合并两链表:LinkList:Merge Two Lists using recursive method
- 将Symbian程序带到前台或后台
- 将数据库中查询得到的信息集转化为List
- oracle数据库字符集设置的查询语法
- 时间格式定义
- 运用指针需细心
- DOS命令详细一览表
- 在Eclipse中CVS操作相关
- 时间过得很快