题目
反转一个单链表。
示例
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head == nullptr || head->next == nullptr)
{
return head;
}
ListNode* pre = head;
ListNode* cur = head->next;
ListNode* r;
head->next = nullptr;
while (cur != nullptr)
{
r = cur->next;
cur->next = pre;
pre = cur;
cur = r;
}
head = pre;
return head;
}
};