题解
很基础的题,与reverseDoublyLinkedList(Hackerrank)那道题作对照,实际上只有一样代码的差别。
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head || !head->next) return head;
ListNode* prev = nullptr;
ListNode* curr = head;
while(curr) {
ListNode* temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
return prev;
}
};