#include<iostream>
struct Node
{
int data;
Node* next;
Node() : data(0), next(NULL) {}
};
//反转逻辑
Node* reverse(Node* p)
{
Node* last = NULL;
while (p)
{
Node* tmp = p->next;
p->next = last;
last = p;
p = tmp;
}
return last;
}
Node* createList(int* list, int size)
{
if (0 == size)
{
return NULL;
}
Node* head = NULL, *p = NULL;
head = p = new Node();
p->data = list[0];
p->next = NULL;
for (int i = 1; i < size; i++)
{
Node* cur = new Node();
cur->data = list[i];
cur->next = NULL;
p->next = cur;
p = cur;
}
return head;
}
void toString(Node* pList)
{
Node* p = pList;
while (p)
{
std::cout << p->data << ",";
p = p->next;
}
std::cout << std::endl;
}
int main()
{
int list1[] = {1,3,9,8};
Node* pList1 = createList(list1, sizeof(list1)/sizeof(int));
toString(pList1);
Node* pRevList = reverse(pList1);
toString(pRevList);
return 0;
}