数据结构编写代码,利用带头结点单链表原来的节点空间逆转一个单链表
时间: 2024-03-13 19:21:01 浏览: 63
带头结点的单链表,反转并输出
```
void reverseList(Node* head) {
if (head == nullptr || head->next == nullptr) {
return;
}
Node* pre = head->next;
Node* cur = pre->next;
pre->next = nullptr;
while (cur != nullptr) {
Node* next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
head->next = pre;
}
```
阅读全文