面试题17:合并两个有序链表,递归和非递归实现

1.输入两个递增排序的链表,合并两个链表并使新链表中的结点仍然是递增排序的。
结果图示:



分析:从合并两个 链表的头节点开始,链表1的头节点小于链表2的头节点的值,因此链表1的头节点是合并之后的头节点。然后在剩余的结点中,链表2的头节点小于链表1的头节点,因此链表2的头节点是剩余结点的头节点,发现合并剩余结点的步骤和之前的过程是一样的,可以使用递归的方法来合并。
图示:

源码:
/*合并两个已经排序的链表*/
#include<iostream>
using namespace std;

struct ListNode
{
	int       m_nValue;
	ListNode* m_pNext;
};

//创建一个节点
ListNode* CreateListNode(int value)
{
	ListNode* pNode = new ListNode();
	pNode->m_nValue = value;
	pNode->m_pNext = NULL;

	return pNode;
}
//打印整个链表
void PrintList(ListNode* pHead)
{
	printf("PrintList starts.\n");

	ListNode* pNode = pHead;
	while (pNode != NULL)
	{
		printf("%d\t", pNode->m_nValue);
		pNode = pNode->m_pNext;
	}

	printf("\nPrintList ends.\n");
}
//打印结点的值
void PrintListNode(ListNode* pNode)
{
	if (pNode == NULL)
	{
		printf("The node is NULL\n");
	}
	else
	{
		printf("The key in node is %d.\n", pNode->m_nValue);
	}
}

//将两个结点连接起来
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
	if (pCurrent == NULL)
	{
		printf("Error to connect two nodes.\n");
		exit(1);
	}

	pCurrent->m_pNext = pNext;
}
//销毁链表
void DestroyList(ListNode* pHead)
{
	ListNode* pNode = pHead;
	while (pNode != NULL)
	{
		pHead = pHead->m_pNext;
		delete pNode;
		pNode = pHead;
	}
}
//合并链表--递归的方法
ListNode* MergeRecursive(ListNode* pHead1, ListNode* pHead2)
{
	if (pHead1 == NULL)
	{
		return pHead2;
	}
	else if (pHead2 == NULL)
	{
		return pHead1;
	}
	ListNode* resultHead = NULL;//合并之后的头节点
	if (pHead1->m_nValue < pHead2->m_nValue)
	{
		resultHead = pHead1;
		resultHead->m_pNext = MergeRecursive(pHead1->m_pNext, pHead2);//递归合并
	}
	else
	{
		resultHead = pHead2;
		resultHead->m_pNext = MergeRecursive(pHead1, pHead2->m_pNext);//递归合并
	}
	return resultHead;
}
//非递归的方法
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
	if (pHead1 == NULL)
	{
		return pHead2;
	}
	else if (pHead2 == NULL)
	{
		return pHead1;
	}
	ListNode* resultHead = NULL;
	if (pHead1->m_nValue < pHead2->m_nValue)//确定头节点
	{
		resultHead = pHead1;
		pHead1 = pHead1->m_pNext;
	}
	else
	{
		resultHead = pHead2;
		pHead2 = pHead2->m_pNext;
	}
	ListNode* pNode = resultHead;//将头节点保存下来
	while (pHead1 != NULL&&pHead2 != NULL)
	{
		if (pHead1->m_nValue < pHead2->m_nValue)
		{
			pNode->m_pNext = pHead1;
			pNode = pHead1;//更新pNode的位置,向前
			pHead1 = pHead1->m_pNext;
		}
		else
		{
			pNode->m_pNext = pHead2;
			pNode = pHead2;
			pHead2 = pHead2->m_pNext;
		}
	}
	if (pHead1 == NULL)//当链表1达到末尾
	{
		pNode = pHead2;
	}
	else if (pHead2==NULL)//链表2达到末尾
	{
		pNode = pHead1;
	}
	return resultHead;

}
int main()
{
	ListNode* pNode1 = CreateListNode(1);
	ListNode* pNode2 = CreateListNode(3);
	ListNode* pNode3 = CreateListNode(5);
	ListNode* pNode4 = CreateListNode(7);
	ListNode* pNode5 = CreateListNode(9);

	ConnectListNodes(pNode1, pNode2);
	ConnectListNodes(pNode2, pNode3);
	ConnectListNodes(pNode3, pNode4);
	ConnectListNodes(pNode4, pNode5);

	printf("the first list: \n");
	PrintList(pNode1);

	ListNode* pNode11 = CreateListNode(2);
	ListNode* pNode12 = CreateListNode(4);
	ListNode* pNode13 = CreateListNode(6);
	ListNode* pNode14 = CreateListNode(8);

	ConnectListNodes(pNode11, pNode12);
	ConnectListNodes(pNode12, pNode13);
	ConnectListNodes(pNode13, pNode14);
	

	printf("\nthe second list: \n");
	PrintList(pNode11);

	printf("after merging:\n");
	ListNode* resultHead = Merge(pNode1, pNode11);
	PrintList(resultHead);

	DestroyList(resultHead);

	system("PAUSE");
	return 0;
}

结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值