首页 > 技术文章 > 合并两个有序列表

blfshiye 2015-09-22 17:30 原文

很多人都在寻找实习、找工作。看看的想法,根据守则手写这个问题。假设有仍处于真正的调试的最佳机会,还有很多细节还需要注意。不多说了,例如,下面的代码记录:

	Node* Merge(Node *h1,Node *h2)
	{
		Node *head,*pCurrent,*head1,*head2;
		head1 = h1;
		head2 = h2;
		if(head1==NULL)
			return head2;
		else if(head2==NULL)
			return head1;		
		
		head = head1->value < head2->value ? head1 : head2;
		if (head == head1)
			head1=head1->next;
		else 
			head2=head2->next;
		pCurrent = head;
		while(head1!= NULL && head2!=NULL)
		{
			if(head1->value <= head2->value)
			{	
				pCurrent->next = head1;
				pCurrent = pCurrent->next;
				head1 = head1->next;
				continue;
			}
			
			if(head1->value > head2->value)
			{
				pCurrent->next =head2;
				pCurrent = pCurrent->next;
				head2 = head2->next;
				continue;
			}
		}
		if(head1==NULL)
		{
			pCurrent->next = head2;
		}
		else if(head2 == NULL)
		{
			pCurrent->next = head1;
		}
		return head;
	};


版权声明:本文博主原创文章。博客,未经同意不得转载。

推荐阅读