关闭

合并两个排好序的链表

167人阅读 评论(0) 收藏 举报
分类:
//合并两个有序的链表,使用递归和非递归的思想
#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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13289次
    • 积分:629
    • 等级:
    • 排名:千里之外
    • 原创:52篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章存档