这两天温度直降,看书的时候手总是不自觉的抖,索性自学完了链表一章
今天重写了链表逆置的题
发现链表题只要画图,弄清逻辑关系,还是很简单的
希望能持之以恒
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
struct Node *next;
}Node;
typedef struct Node LinkList;
LinkList* ReverseList(LinkList *L)
{
LinkList *new_head=NULL;
LinkList *next=NULL;
if(!L||!L->next){
return L;
}
while(L){
next=L->next;
L->next=new_head;
new_head=L;
L=next;
}
return new_head;
}
void Print(LinkList *L)
{
while(L->next){
printf("%d->",L->data);
L=L->next;
}
printf("%d\n",L->data);
}
main()
{
LinkList a,b,c,d,e;
a.data=10;
a.next=&b;
b.data=20;
b.next=&c;
c.data=30;
c.next=&d;
d.data=40;
d.next=&e;
e.data=50;
e.next=NULL;
Print(&a);
LinkList *new_head=NULL;
new_head=ReverseList(&a);
Print(new_head);
}