对于链表的倒置我们可以采用(1)排序方法将所有的数据顺序颠倒,如冒泡法 (2)也可以仅仅将链表中的next指针改变,如下
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
node* reverse(node* head)
{
if(head==NULL||head->next==NULL)
return head;
node* p1,*p2,*p3;
p1=head;
p2=p1->next;
while(p2)
{
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
head->next=NULL;
head=p1;
return head;
}