1. 程式人生 > >單鏈表的逆轉 C語言

單鏈表的逆轉 C語言

#include<stdio.h>
#include<stdlib.h>


typedef struct node{

	int data;
	struct node *next;
}node;

node * reverse(node * head)
{
	node *p1,*p2,*p3;
	if(head==NULL||head->next==NULL)
		return head;
	p1=head;
	p2=p1->next;
	while(p2)
	{
		p3=p2->next;
		p2->next=p1;
		p1=p2;
		p2=p3;
		
	}
	head->next=NULL;
	head=p1;
	return head;
}



void main()
{
	node a[4]={{1,NULL},{2,NULL},{3,NULL},{4,NULL}};
	a[0].next=&a[1];
	a[1].next=&a[2];
	a[2].next=&a[3];
	reverse(&a[0]);
	getchar();

}