131. 一个链表的结点结构,合并非递归和递归

本文介绍了一种将两个已排序的链表合并为一个有序链表的方法,并提供了两种实现方案:迭代和递归。通过示例代码展示了如何创建链表、合并链表以及打印结果。

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

131. 一个链表的结点结构
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。

答案:

我自己写的如下

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "iostream"

using namespace std;

struct Node
{
	int data ;
	Node *next ;
};
typedef struct Node Node ;

void CreatNode(Node** head)
{
	int ndata;
	Node* headTmp = NULL;
	Node* pM = NULL;
	Node* pCur = NULL;
	pCur = headTmp = (Node*)malloc(sizeof(Node));
	headTmp->data = 0;
	headTmp->next = NULL;
	cin>>ndata;
	while ( ndata != 0)
	{
		pM = (Node*)malloc(sizeof(Node));
		pM->data = ndata;
		pM->next = NULL;
		pCur->next = pM;
		pCur = pCur->next;
		cin>>ndata;
	}

	*head = headTmp;
}

Node * Merge(Node *head1 , Node *head2)
{
	Node* pCur1 = head1->next;
	Node* pCur2 = head2->next;

	Node* pResult = (Node*)malloc(sizeof(Node));
	Node* pM = NULL;
	Node* pCur = pResult;

	while (pCur1 && pCur2)
	{
		int nTmp;
		if (pCur1->data > pCur2->data)
		{
			nTmp = pCur2->data;
			pCur2 = pCur2->next;
		}
		else
		{
			nTmp = pCur1->data;
			pCur1 = pCur1->next;
		}

		pM = (Node*)malloc(sizeof(Node));
		pM->data = nTmp;
		pM->next = NULL;

		pCur->next = pM;
		pCur = pCur->next;
	}
	while (pCur2)
	{
		int nTmp = pCur2->data;
		pCur2 = pCur2->next;

		pM = (Node*)malloc(sizeof(Node));
		pM->data = nTmp;
		pM->next = NULL;

		pCur->next = pM;
		pCur = pCur->next;
	}

	while (pCur1)
	{
		int nTmp = pCur1->data;
		pCur1 = pCur1->next;

		pM = (Node*)malloc(sizeof(Node));
		pM->data = nTmp;
		pM->next = NULL;

		pCur->next = pM;
		pCur = pCur->next;
	}
	return pResult;
}


void print(Node *head)
{
	Node* tmp = head->next;
	while (tmp)
	{
		cout<<tmp->data<<' ';
		tmp = tmp->next;
	}
}


int main()
{
	Node* head1 = NULL;
	Node* head2 = NULL;

	CreatNode(&head1);
	CreatNode(&head2);
	Node * head = Merge(head1, head2);
	print(head);
	return 1;
}
后来参考答案修改如下

Node * Merge(Node *head1 , Node *head2)
{
	if (!head1)
	{
		return head2;
	}
	if (!head2)
	{
		return head1;
	}
	Node* head = NULL;
	Node* pCur1 = head1;
	Node* pCur2 = head2;
	
	if (head1->next->data < head2->next->data)
	{
		head = head1;
		pCur1 = head1->next;
	}
	else
	{
		head = head2;
		pCur2 = head2->next;
	}
	Node* pCur = head->next;

	while (pCur1 && pCur2)
	{
		if (pCur1->data < pCur2)
		{
			pCur->next = pCur1;
			pCur = pCur->next;
			pCur1 = pCur1->next;
		}
		else
		{
			pCur->next = pCur2;
			pCur = pCur->next;
			pCur2 = pCur2->next;
		}
	}
	if (pCur1)
	{
		pCur->next = pCur1;
	}
	if (pCur2)
	{
		pCur->next = pCur2;
	}
}


递归方法如下

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "iostream"

using namespace std;

struct Node
{
	int data ;
	Node *next ;
};
typedef struct Node Node ;

void CreatNode(Node** head)
{
	int ndata;
	Node* headTmp = NULL;
	Node* pM = NULL;
	Node* pCur = NULL;
	pCur = headTmp = (Node*)malloc(sizeof(Node));
	headTmp->data = 0;
	headTmp->next = NULL;
	cin>>ndata;
	while ( ndata != 0)
	{
		pM = (Node*)malloc(sizeof(Node));
		pM->data = ndata;
		pM->next = NULL;
		pCur->next = pM;
		pCur = pCur->next;
		cin>>ndata;
	}

	*head = headTmp;
}

Node* MergeDiDui(Node* head1, Node* head2)
{
	if (head1 == NULL)
	{
		return head2;
	}
	if (head2 == NULL)
	{
		return head1;
	}
	Node* head = NULL;
	if (head1->data < head2->data)
	{
		head = head1;
		head->next = MergeDiDui(head1->next, head2);
	}
	else
	{
		head = head2;
		head->next = MergeDiDui(head1, head2->next);
	}
	return head;
}



void print(Node *head)
{
	Node* tmp = head->next;
	while (tmp)
	{
		cout<<tmp->data<<' ';
		tmp = tmp->next;
	}
}


int main()
{
	Node* head1 = NULL;
	Node* head2 = NULL;

	CreatNode(&head1);
	CreatNode(&head2);
	Node * head = MergeDiDui(head1, head2);
	print(head);
	return 1;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值