Smallest and easisest C++ code. (5 lines)


#1
ListNode* Solution::swapPairs(ListNode* A) {
if(!A || !A->next)return A;
ListNode *scnd= A->next;
A->next=swapPairs(scnd->next);
scnd->next=A;
return scnd;
}

#2

Great Solution there, but it isn’t constant space. There will be a function call stack and the space complexity will go O(n)


#3

Oh yes. Thats a nice observation!
Thanks for enlightening!