A sample recursion in C++.


  • 0

    class Solution {
    public:
    ListNode* swapPairs(ListNode* head) {
    if (head == nullptr || head->next == nullptr) return head;
    ListNode* res = head->next;
    head->next = swapPairs(res->next);
    res->next = head;
    return res;
    }
    };


Log in to reply
 

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