剑指offer 17---合并两个排序的链表

36 篇文章 1 订阅
25 篇文章 0 订阅

一.链表1:1 3 5 7 9    链表2:2 4 6 8 10      升序排列

#include <iostream>
#include <Windows.h>
using namespace std;

//合并两个有序链表,假设链表是升序排列
//链表1:1,3,5,7,9      链表2:2,4,6,8,10
struct ListNode
{
	int _value;
	ListNode* _next;
	ListNode(const int& value)
		:_value(value)
		, _next(NULL)
	{}
};

ListNode* Merge(ListNode* pHead1, ListNode* pHead2)    //链表合并
{
	if (pHead1 == NULL)
	{
		return pHead2;
	}
	else if (pHead2 == NULL)
	{
		return pHead1;
	}
	ListNode* MergeHead = NULL;
	if (pHead1->_value < pHead2->_value)
	{
		MergeHead = pHead1;
		MergeHead->_next = Merge(pHead1->_next, pHead2);
	}
	else
	{
		MergeHead = pHead2;
		MergeHead->_next = Merge(pHead1, pHead2->_next);
	}
	return MergeHead;
}

int main()
{
	ListNode* pHead1 = new ListNode(1);
	ListNode* cur1 = pHead1;
	for (int i = 3; i < 10; i = i + 2)
	{
		ListNode* temp1 = new ListNode(i);
		cur1->_next = temp1;
		cur1 = temp1;
	}
	ListNode* pHead2 = new ListNode(2);
	ListNode* cur2 = pHead2;
	for (int j = 4; j <= 10; j = j + 2)
	{
		ListNode* temp2 = new ListNode(j);
		cur2->_next = temp2;
		cur2 = temp2;
	}
	ListNode* MergeNode = Merge(pHead1, pHead2);
	cout << "合并后的链表为:" << "";
	while (MergeNode != NULL)
	{
		cout << MergeNode->_value << "->";
		MergeNode = MergeNode->_next;
	}
	cout << endl;
	system("pause");
	return 0;
}




二.对没有规律的有序链表合并

链表1:12 16 18 20 89 67 78 81 99   链表2: 2 4 66 89 99 108 10000
 

#include <iostream>
#include <Windows.h>
using namespace std;

//合并两个有序链表,假设链表是升序排列
//链表1:12 16 18 20 89 67 78 81 99   链表2: 2 4 66 89 99 108 10000
struct ListNode
{
	int _value;
	ListNode* _next;
	ListNode(const int& value)
		:_value(value)
		, _next(NULL)
	{}
};

ListNode* Merge(ListNode* pHead1, ListNode* pHead2)    //链表合并
{
	if (pHead1 == NULL)
	{
		return pHead2;
	}
	else if (pHead2 == NULL)
	{
		return pHead1;
	}
	ListNode* MergeHead = NULL;
	if (pHead1->_value < pHead2->_value)
	{
		MergeHead = pHead1;
		MergeHead->_next = Merge(pHead1->_next, pHead2);
	}
	else
	{
		MergeHead = pHead2;
		MergeHead->_next = Merge(pHead1, pHead2->_next);
	}
	return MergeHead;
}

int main()
{
	//链表1:12 16 18 20 33 67 78 81 99   链表2: 2 3 4 66 89 99 108 10000
	ListNode* pHead1 = new ListNode(12);
	ListNode* cur1 = pHead1;
	ListNode* temp1 = new ListNode(16);
	cur1->_next = temp1;
	cur1 = temp1;
	temp1 = new ListNode(18);
	cur1->_next = temp1;
	cur1 = temp1;
	temp1 = new ListNode(20);
	cur1->_next = temp1;
	cur1 = temp1;
	temp1 = new ListNode(33);
	cur1->_next = temp1;
	cur1 = temp1;
	temp1 = new ListNode(67);
	cur1->_next = temp1;
	cur1 = temp1;
	temp1 = new ListNode(78);
	cur1->_next = temp1;
	cur1 = temp1;
	temp1 = new ListNode(81);
	cur1->_next = temp1;
	cur1 = temp1;
	temp1 = new ListNode(99);
	cur1->_next = temp1;
	cur1 = temp1;
	
	//2 4 66 89 99 108 10000
	ListNode* pHead2 = new ListNode(2);
	ListNode* cur2 = pHead2;
	ListNode* temp2 = new ListNode(3);
	cur2->_next = temp2;
	cur2 = temp2;
	temp2 = new ListNode(4);
	cur2->_next = temp2;
	cur2 = temp2;
	temp2 = new ListNode(66);
	cur2->_next = temp2;
	cur2 = temp2;
	temp2 = new ListNode(89);
	cur2->_next = temp2;
	cur2 = temp2;
	temp2 = new ListNode(99);
	cur2->_next = temp2;
	cur2 = temp2;
	temp2 = new ListNode(108);
	cur2->_next = temp2;
	cur2 = temp2;
	temp2 = new ListNode(10000);
	cur2->_next = temp2;
	cur2 = temp2;

	ListNode* MergeNode = Merge(pHead1, pHead2);
	cout << "合并后的链表为:" << "";
	while (MergeNode != NULL)
	{
		cout << MergeNode->_value << "->";
		MergeNode = MergeNode->_next;
	}
	cout << endl;
	system("pause");
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值