合并两个排好序的链表

//合并两个有序的链表,使用递归和非递归的思想
#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 ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
    if(pCurrent == NULL)
		return;

    pCurrent->m_pNext = pNext;
}
void print(ListNode* pHead)
{
	if(pHead==NULL)
		return;
	while(pHead!=NULL)
	{
		cout<<pHead->m_nValue <<" ";
		pHead=pHead->m_pNext;
	}
}


//递归的思想
ListNode* MergeList(ListNode* list1,ListNode* list2)
{
	if(list1==NULL)
		return list2;
	else if(list2==NULL)
		return list1;
	if(list1==NULL&&list2==NULL)
		return NULL;

	ListNode *pHead=NULL;
	if(list1->m_nValue<list2->m_nValue )
	{
		pHead=list1;
		pHead->m_pNext=MergeList(list1->m_pNext ,list2);
	}
	else
	{
		pHead=list2;
		pHead->m_pNext=MergeList(list1,list2->m_pNext );
	}
	return pHead;
}
//非递归的做法
ListNode * Merge(ListNode *head1,ListNode *head2)
{
	if ( head1 == NULL)
		return head2 ;
	if ( head2 == NULL)
		return head1 ;
	if(head1==NULL&&head2==NULL)
		return NULL;
	
	ListNode *head = NULL ;
	ListNode *p1 = NULL;
	ListNode *p2 = NULL;
	if ( head1->m_nValue < head2->m_nValue )
	{
		head = head1 ;
		p1 = head1->m_pNext;
		p2 = head2 ;
	}
	else
	{
		head = head2 ;
		p2 = head2->m_pNext ;
		p1 = head1 ;

	}
	ListNode *pcurrent = head ;
	while ( p1 != NULL && p2 != NULL)
	{
		if ( p1->m_nValue <= p2->m_nValue )
		{
			pcurrent->m_pNext = p1 ;
			pcurrent = p1 ;
			p1 = p1->m_pNext ;
		}
		else
		{
			pcurrent->m_pNext = p2 ;
			pcurrent = p2 ;
			p2 = p2->m_pNext ;
		}
	}
	if ( p1 != NULL )
		pcurrent->m_pNext = p1 ;
	if ( p2 != NULL )
		pcurrent->m_pNext = p2 ;

	return head ;

}

void main()
{
	ListNode* pNode3 = CreateListNode(3);
    ListNode* pNode5 = CreateListNode(5);
    ListNode* pNode8 = CreateListNode(8);
	ListNode* pNode11 = CreateListNode(11);

    ConnectListNodes(pNode3, pNode5);
    ConnectListNodes(pNode5, pNode8);
	ConnectListNodes(pNode8, pNode11);


	ListNode* pNode2 = CreateListNode(2);
    ListNode* pNode6 = CreateListNode(6);
    ListNode* pNode8a = CreateListNode(8);
	ListNode* pNode9 = CreateListNode(9);
	ListNode* pNode11a = CreateListNode(11);
    ListNode* pNode15 = CreateListNode(15);
    ListNode* pNode20 = CreateListNode(20);

	ConnectListNodes(pNode2, pNode6);
    ConnectListNodes(pNode6, pNode8a);
	ConnectListNodes(pNode8a, pNode9);
	ConnectListNodes(pNode9, pNode11a);
    ConnectListNodes(pNode11a, pNode15);
	ConnectListNodes(pNode15, pNode20);

	print(pNode3);
	cout<<endl;
	print(pNode2);
	cout<<endl;

	//递归的思想做的合并
//	ListNode *pNode=MergeList(pNode3,pNode2);
//	print(pNode);
//	cout<<endl;

	//非递归的思想做的合并
	ListNode *pNodes=Merge(pNode3,pNode2);
	print(pNodes);
	cout<<endl;
}
	

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值