#include <iostream>
using namespace std;
//结点
struct Node
{
int value;
Node* next;
};
//尾插法
Node* create_list(int v, int len)
{
Node* ret = NULL;
Node* slider = NULL;
for(int i=0; i<len; i++)
{
Node* n = new Node();
n->value = v++;//头结点
n->next = NULL;
if(slider == NULL)
{
slider = n;//slider和头结点挂接
ret = n;//ret指向头结点
}
else
{
slider->next = n; //尾插法
slider = n;//移动slider指向新插入的结点
}
}
return ret;//返回链表头结点
}
void destory_list(Node* list)
{
while(list)
{
Node* del = list;
list = list->next;
delete del;
}
}
void print_list(Node* list)
{
while(list)
{
cout << list->value << "->";
list = list->next;
}
cout << "NULL" << endl;
}
Node* reverse_non(Node* list)
{
if(list == NULL || list->next == NULL)
{
return list;
}
else
{
Node* guard = list->next;//保存list的下一个结点
list->next = NULL;//头结点的指向空
while(guard != NULL)
{
Node* tmp = guard->next; //指向guard的下一个元素 也就是list的下一个下一个结点 1
guard->next = list; //然后把guard指向list 2
list = guard; //移动list 3
guard = tmp; //移动guard 4
}
return list;
}
}
int main()
{
Node* list = create_list(1, 6);
print_list(list);
list = reverse_non(list);
print_list(list);
destory_list(list);
return 0;
}