单链表合并
2011-11-16 18:12
92 查看
已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)
#include <stdio.h>
#include <stdlib.h>
typedef struct _Node{
int data;
_Node* next;
}Node, *LinkList;
bool CreateList(LinkList &head, const int *data, const int len)
{
Node *cur = NULL;
Node *next = NULL;
int i;
cur = (LinkList)malloc(sizeof(Node));
if(cur == NULL)
return false;
cur->data = data[0];
cur->next = NULL;
head = cur;
for(i=1; i<len; i++)
{
next = (LinkList)malloc(sizeof(Node));
if(next == NULL)
return false;
next->data = data[i];
next->next = NULL;
cur->next = next;
cur = cur->next;
}
return true;
}
void PrintList(LinkList head)
{
while(head != NULL)
{
printf(" %d", head->data);
head = head->next;
}
}
//一般合并
LinkList MergeList(LinkList head1, LinkList head2)
{
if (head1 == NULL)
return head2;
if (head2 == NULL)
return head1;
LinkList head = NULL;
LinkList p1 = NULL;
LinkList p2 = NULL;
if ( head1->data < head2->data )
{
head = head1;
p1 = head1->next;
p2 = head2;
}
else
{
head = head2;
p2 = head2->next;
p1 = head1;
}
LinkList cur = head;
while ( (p1!=NULL) && (p2!=NULL) )
{
if ( p1->data <= p2->data )
{
cur->next = p1;
cur = p1;
p1 = p1->next;
}
else
{
cur->next = p2;
cur = p2;
p2 = p2->next;
}
}
if ( p1 != NULL )
cur->next = p1;
if ( p2 != NULL )
cur->next = p2;
return head;
}
//递归合并
LinkList RMergeList(LinkList head1, LinkList head2)
{
if (head1 == NULL)
return head2;
if (head2 == NULL)
return head1;
LinkList head = NULL;
if (head1->data < head2->data)
{
head = head1;
head->next = RMergeList(head1->next, head2);
}
else
{
head = head2;
head->next = RMergeList(head1, head2->next);
}
return head;
}
void main( void )
{
int data1[] = {1, 2, 3, 5};
int len1 = sizeof(data1)/sizeof(int);
int data2[] = {2, 4, 6};
int len2 = sizeof(data2)/sizeof(int);
LinkList head;
LinkList head1;
LinkList head2;
if( !CreateList(head1, data1, len1) )
{
printf("创建链表失败!\n");
return;
}
if( !CreateList(head2, data2, len2) )
{
printf("创建链表失败!\n");
return;
}
printf("链表1:");
PrintList(head1);
printf("\n");
printf("链表2:");
PrintList(head2);
printf("\n");
head = MergeList(head1, head2);
printf("一般合并:");
PrintList(head);
printf("\n");
if( !CreateList(head1, data1, len1) )
{
printf("创建链表失败!\n");
return;
}
if( !CreateList(head2, data2, len2) )
{
printf("创建链表失败!\n");
return;
}
printf("链表1:");
PrintList(head1);
printf("\n");
printf("链表2:");
PrintList(head2);
printf("\n");
head = RMergeList(head1, head2);
printf("递归合并:");
PrintList(head);
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
typedef struct _Node{
int data;
_Node* next;
}Node, *LinkList;
bool CreateList(LinkList &head, const int *data, const int len)
{
Node *cur = NULL;
Node *next = NULL;
int i;
cur = (LinkList)malloc(sizeof(Node));
if(cur == NULL)
return false;
cur->data = data[0];
cur->next = NULL;
head = cur;
for(i=1; i<len; i++)
{
next = (LinkList)malloc(sizeof(Node));
if(next == NULL)
return false;
next->data = data[i];
next->next = NULL;
cur->next = next;
cur = cur->next;
}
return true;
}
void PrintList(LinkList head)
{
while(head != NULL)
{
printf(" %d", head->data);
head = head->next;
}
}
//一般合并
LinkList MergeList(LinkList head1, LinkList head2)
{
if (head1 == NULL)
return head2;
if (head2 == NULL)
return head1;
LinkList head = NULL;
LinkList p1 = NULL;
LinkList p2 = NULL;
if ( head1->data < head2->data )
{
head = head1;
p1 = head1->next;
p2 = head2;
}
else
{
head = head2;
p2 = head2->next;
p1 = head1;
}
LinkList cur = head;
while ( (p1!=NULL) && (p2!=NULL) )
{
if ( p1->data <= p2->data )
{
cur->next = p1;
cur = p1;
p1 = p1->next;
}
else
{
cur->next = p2;
cur = p2;
p2 = p2->next;
}
}
if ( p1 != NULL )
cur->next = p1;
if ( p2 != NULL )
cur->next = p2;
return head;
}
//递归合并
LinkList RMergeList(LinkList head1, LinkList head2)
{
if (head1 == NULL)
return head2;
if (head2 == NULL)
return head1;
LinkList head = NULL;
if (head1->data < head2->data)
{
head = head1;
head->next = RMergeList(head1->next, head2);
}
else
{
head = head2;
head->next = RMergeList(head1, head2->next);
}
return head;
}
void main( void )
{
int data1[] = {1, 2, 3, 5};
int len1 = sizeof(data1)/sizeof(int);
int data2[] = {2, 4, 6};
int len2 = sizeof(data2)/sizeof(int);
LinkList head;
LinkList head1;
LinkList head2;
if( !CreateList(head1, data1, len1) )
{
printf("创建链表失败!\n");
return;
}
if( !CreateList(head2, data2, len2) )
{
printf("创建链表失败!\n");
return;
}
printf("链表1:");
PrintList(head1);
printf("\n");
printf("链表2:");
PrintList(head2);
printf("\n");
head = MergeList(head1, head2);
printf("一般合并:");
PrintList(head);
printf("\n");
if( !CreateList(head1, data1, len1) )
{
printf("创建链表失败!\n");
return;
}
if( !CreateList(head2, data2, len2) )
{
printf("创建链表失败!\n");
return;
}
printf("链表1:");
PrintList(head1);
printf("\n");
printf("链表2:");
PrintList(head2);
printf("\n");
head = RMergeList(head1, head2);
printf("递归合并:");
PrintList(head);
printf("\n");
}
相关文章推荐
- 将k个有序链表合并成一个有序链表
- c语言版数据结构(奇迹冬瓜)-链表实战(2)合并两有序线性表
- 将两个有序链表合并成一个有序链表——搜狐畅游笔试题归来
- 面试杂题(八)合并两个递增链表
- LintCode—合并两个排序链表(165)
- 单链表的排序合并实现
- 【剑指offer】Q17:合并两个排序的链表
- 剑指offer----合并两个有序链表
- Language 晓彤 蒋 165. 合并两个排序链表 拼接,递归,新建
- 递归实现合并两个有序链表成一个链表依然有序
- 有序链表合并
- 合并两个排序的链表(剑指Offer 第 15 题)
- 合并两个递增排序的链表
- 合并两个排序好的链表/链表去重/链式快排
- lintcode,合并两个排序链表
- 合并两个排序的链表(剑指offer)
- 合并两个有序链表的递归与非递归
- K个有序链表共N个结点在O(NlgK)时间合并为一个新的有序链表头文件C语言
- 在单链表中将两个链表合并,合并之后的链表使用的是输入链表的节点空间,合并之后输入链表变为空表
- 九度1519题 合并两个有序链表java实现,(也是剑指offer中的题)