### 思路
1. 初始化三个指针:`prev`(前驱节点),`curr`(当前节点),`next`(后继节点)。
2. 遍历链表,将当前节点的`next`指针指向前驱节点,实现反转。
3. 移动三个指针,继续反转下一个节点,直到遍历完整个链表。
4. 最后,将头节点指向新的头节点(即原链表的最后一个节点)。
### 伪代码
```
function reverseList(L):
initialize prev as NULL
initialize curr as L->next
while curr is not NULL:
next = curr->next
curr->next = prev
prev = curr
curr = next
L->next = prev
```
### C++代码
#include <iostream>
using namespace std;
struct LNode {
int data;
LNode *next;
};
void createList(LNode *&L, int n) {
LNode *r, *p;
r = L = new LNode;
L->next = NULL;
for (int i = 1; i <= n; i++) {
p = new LNode;
cin >> p->data;
p->next = NULL;
r->next = p;
r = p;
}
}
void trv(LNode *L) {
L = L->next;
while (L) {
cout << L->data << ' ';
L = L->next;
}
cout << endl;
}
void reverseList(LNode *&L) {
LNode *prev = NULL, *curr = L->next, *next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
L->next = prev;
}
int main() {
int n;
LNode *L;
cin >> n;
createList(L, n);
reverseList(L);
trv(L);
return 0;
}