逆转单项链表

SNode * Reverse(SNode * h)
{
	SNode * next = NULL;
	while(h!=NULL)
	{
		SNode * t = h;
		h = h ->next;
		t->next = next;
		next = t;
	}
	return next;
}

int main()
{
	SNode * h = new SNode(10);
	SNode * t = h;
	t->next = new SNode(20);
	t = t->next;
	t->next = new SNode(30);
	t = t->next;
	t->next = new SNode(40);
	t = t->next;
	t->next = new SNode(50);
	t = t->next;
	t->next =0;
	t = h;
	while(t!=NULL)
	{
		cout<<t->data<<endl;
		t = t->next;
	}
	h = Reverse(h);
	t = h;
	while(t!=NULL)
	{
		cout<<t->data<<endl;
		t = t->next;
	}
	return 0;
}

posted on 2010-12-02 14:15  SammyLan  阅读(203)  评论(0编辑  收藏  举报

导航