#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
void print(node * const head)
{
if(head == NULL)
{
return;
}
node *p = head;
while(p != NULL)
{
cout<<"node:"<<p->data<<endl;
p = p->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 p1;
}
int main()
{
node a;
node b;
node c;
a.data = 1;
a.next = &b;
b.data = 2;
b.next = &c;
c.data = 3;
c.next = NULL;
node *head = &a;
print(head);
cout<<endl;
head = reverse(head);
print(head);
head = NULL;
return 0;
}
单链表的反转
最新推荐文章于 2024-10-03 21:03:30 发布