[2020.10.14] 单链的逆转
编程实现单链表的逆转函数
http://www.nowcoder.com/questionTerminal/33e343e6d5df40bc9b3a26b01c82a718
实现单链表的逆转函数,输入一个链表,反转链表后,返回翻转之后的链表。
类似题目很多,这里仅记录日后查看:
/*
只需要完成逆置链表函数
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
// 两种特殊情况
// 空
if (pHead == nullptr) {
return nullptr;
}
// 单一
if (pHead->next == nullptr) {
return pHead;
}
auto tmp = pHead;
auto res = pHead;
res = nullptr;
while (pHead != nullptr) {
tmp = pHead;
pHead = pHead->next;
tmp->next = res;
res = tmp;
}
return res;
}
}; 
