一个比较好理解的方法,先在这里留个坑,周末补充一下内容
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
#include <cstddef>
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if (pHead == nullptr) {
return nullptr;
}
ListNode *cur = pHead;
ListNode *pre = nullptr;
while (cur != nullptr) {
ListNode *tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
};

查看13道真题和解析