不带头节点单链表的反转

//注意:head为头指针(适用于不带头节点的单链表)
void List::Reverse1()
{
	if(head==NULL || head->next==NULL)
		return ;

	Node *pre,*cur,*nex;
	pre=head;
	cur=pre->next;
	nex=cur->next;
	while(nex)
	{
		cur->next=pre;
		pre=cur;
		cur=nex;
		nex=nex->next;
	}
	cur->next=pre;
	head->next=NULL;
	head=cur;
}

猜你喜欢

转载自blog.csdn.net/Henry313/article/details/88614631