- 🧩 Problem link: Leetcode
- 🚦 Difficulty: 🟢 Easy
- Set
curr->nextnode to be theprevone, always updating in each loopprevwithcurrnode - But using recursion
- Time: O(n)
- Space: O(n)
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (!head) {
return nullptr;
}
ListNode* newHead = head;
if (head->next) {
newHead = reverseList(head->next);
head->next->next = head;
}
head->next = nullptr;
return newHead;
}
};