I watched a two pointer video,
but I thought its recursion is better.
Conditions:
1. generally, “loop” can be written as the recursion.
Mental cultivation:
1. A basic condition (trivial result)
2. Asking for the subproblem result **
3. Do something in current level of recurrsion
4. Return result **
The results of 2. & 4. are the same meaning.
Examples:
Template:
ListNode* reverseList(ListNode* head) {
// 1. A basic condition (trivial result)
if(head == nullptr || head->next == nullptr){
return head;
}
// 2. Asking for the subproblem result **
ListNode* reversed_result = reverseList(head->next);
// 3. Do something in current level of recurrsion
head->next->next = head;
head->next = nullptr;
// 4. Return result **
return reversed_result;
}