个人代码如下:
#include <iostream>
using namespace std;
typedef struct STNode
{
int data;
struct STNode *next;
}Node;
void CreateList(Node *&Root, int len)
{
Node *pNewNode = NULL;
for(int i = 0; i < len; ++i)
{
pNewNode = new Node;
pNewNode->data = i;
pNewNode->next = Root;
Root = pNewNode;
}
}
void PrintList(const Node *pRoot)
{
while(NULL != pRoot)
{
cout<< pRoot->data << " ";
pRoot = pRoot->next;
}
}
void RevList(Node *&Root)
{
assert(NULL != Root);
Node *pFront = NULL;
Node *pNext = NULL;
while(NULL != Root)
{
pNext = Root->next;
Root->next = pFront;
pFront = Root;
Root = pNext;
}
Root = pFront;
}
int main()
{
Node *Root = NULL;
CreateList(Root, 5);
RevList(Root);
PrintList(Root);
}
输出: