share a recursion method in C++


  • 0
    Z
        ListNode* reverseBetween(ListNode* head, int m, int n) {
            if (!head) return NULL;
            if (m > 1) {
                head->next = reverseBetween(head->next, m - 1, n - 1);
                return head;
            }
            if (n > 1) {
                ListNode *ret = reverseBetween(head->next, m, n - 1);
                ListNode *tmp = head->next->next;
                head->next->next = head;
                head->next = tmp;
                return ret;
            }
            return head;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.