#include <iostream>
using namespace std;
typedef struct Node
{
int num;
Node *next;
}Node;
Node * reverse(Node *head)
{
if(head == NULL){
return head;
}
if (head->next == NULL)
{
return head;
}
cout<<"reverse in "<<head->num<<endl;
Node *ph = reverse(head->next);
cout<<"reverse out "<<head->num<<endl;
head->next->next = head;
head->next = NULL;
return ph;
}
int main(){
Node p1,p2,p3,p4;
p1.next = &p2;
p2.next = &p3;
p3.next = &p4;
p4.next = NULL;
p1.num = 100;
p2.num = 200;
p3.num = 300;
p4.num = 400;
Node *head;
head = &p1;
while (head != NULL)
{
cout<<head->num<<"\n";
head=head->next;
}
head = reverse(&p1);
while (head != NULL)
{
cout<<head->num<<"\n";
head=head->next;
}
system("pause");
return 0;
}
今天2了,看一个反向链表都没看懂,就写了下代码来测一下对不对。
看一个反向链表都没看懂,就写了下代码来测一下对不对
最新推荐文章于 2023-01-10 16:51:47 发布