两个有序的链表合成一个有序的链表

已知两个链表head1 和 head2 都各自升序排序,请合成一个链表并且依然有序。使用递归和非递归方法如下:

用递归方法:

#include<iostream>
using namespace std;
typedef struct node
{
	int data;
	struct node *next;
}Node;
Node *Merge(Node *head1,Node*head2)
{
	Node *head=NULL;
	if (head1==NULL)
	{
		return head2;
	}
	if (head2==NULL)
	{
		return head1;
	}
	if (head1->data<head2->data)
	{
		head=head1;
		head->next=Merge(head1->next,head2);
	}
	else 
	{
		head=head2;
		head->next=Merge(head1,head2->next);
	}
	return head;//注意这里返回的是第一个节点
}
int main(int argc,char *argv[])
{ 
	Node *head1,*temp1,*p,*q,*temp2,*head2,*temp;
	int i;
	head1=(Node*)malloc(sizeof(Node));
	head2=(Node*)malloc(sizeof(Node));
	temp=(Node*)malloc(sizeof(Node));
	p=temp1=head1;
	q=temp2=head2;
	for (i=0;i<5;i++)
	{
		temp->data=i+1;
		head1->next=temp;
		head1=temp;
		temp=(Node*)malloc(sizeof(Node));
	}
	head1->next=NULL;
	for (i=0;i<7;i++)
	{
		temp->data=i+2;
		head2->next=temp;
		head2=temp;
		temp=(Node*)malloc(sizeof(Node));
	}
	head2->next=NULL;
	cout<<"合并前:head1"<<endl;
	temp1=temp1->next;
	while (temp1!=NULL)
	{
		cout<<temp1->data<<" ";
		temp1=temp1->next;
	}
	cout<<endl<<"合并前:head2"<<endl;
	temp2=temp2->next;
	while (temp2!=NULL)
	{
		cout<<temp2->data<<" ";
		temp2=temp2->next;
	}
	cout<<endl;
	temp=Merge(p->next,q->next);
	cout<<endl<<"合并之后:head"<<endl;
	while (temp!=NULL)
	{
		cout<<temp->data<<" ";
		temp=temp->next;
	}
	cout<<endl;
	free(temp);
	free(head1);
	free(head2);
	return 0;
}


运行结果:

合并前:head1
1 2 3 4 5
合并前:head2
2 3 4 5 6 7 8

合并之后:head
1 2 2 3 3 4 4 5 5 6 7 8

 非递归方法如下:

#include<iostream>
using namespace std;
typedef struct node
{
	int data;
	struct node *next;
}Node;
Node *Merge(Node *head1,Node*head2)
{
	Node *head=(Node*)malloc(sizeof(Node));
	Node *temp=head;
	if (NULL == head1)
	{
		return head2;
	}
	else 
	{
		if (NULL == head2)
		{
			return head1;
		}
	}
	while (head1!=NULL&&head2!=NULL)
	{
		if (head1->data<head2->data)
		{
			temp->next=head1;
			temp=temp->next;
			head1=head1->next;
		}
		else
		{
			temp->next=head2;
			temp=temp->next;
			head2=head2->next;
		}
	}
	if (NULL==head1)
	{
		temp->next=head2;
	}
	else
	{
		temp->next=head1;
	}
	return head->next;
}
int main(int argc,char *argv[])
{ 
	Node *head1,*temp1,*p,*q,*temp2,*head2,*temp;
	int i;
	head1=(Node*)malloc(sizeof(Node));
	head2=(Node*)malloc(sizeof(Node));
	temp=(Node*)malloc(sizeof(Node));
	p=temp1=head1;
	q=temp2=head2;
	for (i=0;i<5;i++)
	{
		temp->data=i+1;
		head1->next=temp;
		head1=temp;
		temp=(Node*)malloc(sizeof(Node));
	}
	head1->next=NULL;
	for (i=0;i<7;i++)
	{
		temp->data=i+2;
		head2->next=temp;
		head2=temp;
		temp=(Node*)malloc(sizeof(Node));
	}
	head2->next=NULL;
	cout<<"合并前:head1"<<endl;
	temp1=temp1->next;
	while (temp1!=NULL)
	{
		cout<<temp1->data<<" ";
		temp1=temp1->next;
	}
	cout<<endl<<"合并前:head2"<<endl;
	temp2=temp2->next;
	while (temp2!=NULL)
	{
		cout<<temp2->data<<" ";
		temp2=temp2->next;
	}
	cout<<endl;
	temp=Merge(p->next,q->next);
	cout<<endl<<"合并之后:head"<<endl;
	while (temp!=NULL)
	{
		cout<<temp->data<<" ";
		temp=temp->next;
	}
	cout<<endl;
	free(temp);
	free(head1);
	free(head2);
	return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是C语言的代码实现: ```c #include <stdio.h> #include <stdlib.h> struct ListNode { int val; struct ListNode *next; }; struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){ struct ListNode *dummy = (struct ListNode*)malloc(sizeof(struct ListNode)); // 创建虚拟头节点 struct ListNode *cur = dummy; // cur指针指向新链表的最后一个节点,初始指向虚拟头节点 while (l1 && l2) { if (l1->val < l2->val) { cur->next = l1; l1 = l1->next; } else { cur->next = l2; l2 = l2->next; } cur = cur->next; } cur->next = l1 ? l1 : l2; // 如果l1或l2还有剩余节点,直接将其添加到新链表的末尾 return dummy->next; // 返回新链表的头节点 } int main() { struct ListNode a[5] = {{1, NULL}, {3, NULL}, {5, NULL}, {7, NULL}, {9, NULL}}; // 创建链表A struct ListNode b[5] = {{2, NULL}, {4, NULL}, {6, NULL}, {8, NULL}, {10, NULL}}; // 创建链表B for (int i = 0; i < 4; i++) { a[i].next = &a[i+1]; b[i].next = &b[i+1]; } struct ListNode *head = mergeTwoLists(a, b); // 合并两个有序链表 while (head) { // 遍历新链表并输出 printf("%d ", head->val); head = head->next; } return 0; } ``` 在这个实现中,我们使用了结构体来表示链表节点,其中val表示节点的值,next表示指向下一个节点的指针。在main函数中,我们首先创建了两个有序链表A和B,然后使用for循环将它们连接成链表。最后,调用mergeTwoLists函数合并两个有序链表,并将新链表的头节点赋值给head指针。最后,我们再次遍历新链表并输出其中的节点值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值