class Solution {
public:
ListNode *swapPairs(ListNode *head) {
if(NULL == head  NULL == head>next)
return head;
ListNode *p = head;
ListNode *q = p>next;
ListNode *k = q>next;
q>next = p;
p>next = swapPairs(k);
return q;
}
};
My answer using recursion

It's fun to solve things recursively, but if you do, just make sure that you recognize that recursive solutions for linear problems like this are not recommended in practice, because you're creating a stack frame for every element in the list, and for even moderately sized lists, this can cause a stack overflow. It's also not as performant as the iterative solution. For tree's and other structures where you can get a log(n) runtime, recursion is more pragmatic.

Your solution have too many temporary variable, please have a look at the follow one:
class Solution { public: ListNode *swapPairs(ListNode *head) { if (head == NULL  head>next == NULL) return head; ListNode *result = head>next; head>next = swapPairs(head>next>next); result>next = head; return result; } };

Thanks for your post. However it would be better to share solution with correct code format and elaborated thoughts. Please read the Discuss FAQ for more info. Take a look at good sharing example