15.Reverse_Linked_List
2024年2月2日小于 1 分钟约 65 字
// 输入一个链表,反转链表后,输出新链表的表头。
struct ListNode {
int val;
struct ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *cur = pHead, *pre = nullptr, *next = nullptr;
while (cur) {
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
};