您的位置:首页 > 其它

24 单链表就地逆置,合并链表

2014-09-18 19:30 246 查看
/*
第 24  题:
链表操作,
(1).单链表就地逆置,
(2)合并链表
*/

node * reverseNonrecurse(node *head)
{
	if(head==NULL) return head;
	
	node *p=head,*previous=NULL,*next=NULL;
	
	while(p->next!=NULL)
	{
		next=p->next;//保存下一个 
		p->next=previous;//p下一个为他前面的
		previous=p;
		p=next; 
	}
	p->next=previous;
	return p;
}

//两个排好序的合并 
Node *merge(Node *h1, Node *h2) 
{
	if (h1==NULL) return h2;
	if (h2==NULL) return h1;
	Node *head;
	if (h1->data>h2->data) //谁做头 
	{
		head=h2;
		h2=h2->next;
	} 
	else 
	{
		head=h1;
		h1=h1->next;
	}
	
	Node *current=head;
	
	while(h1!=NULL||h2!=NULL) 
	{
		if(h1==NULL || (h2!=NULL&&h2->data<h1->data))//h1空,或者h2不为空,并且值小于h1 
		{
			current->next=h2;
			h2=h2->next; 
			current=current->next;
		} 
		else 
		{
			current->next=h1;
			h1=h1->next;
			current=current->next;
		} 
	}
	current->next = NULL;
	return head; 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: