A recursive solution


  • 1
    P
    class Solution {
    public:
    ListNode* swapPairs(ListNode* head) {
    	if(!head || !head->next)return head;
        
    	ListNode* temp = head->next;
    	head->next = swapPairs(temp->next);
    	temp->next = head;
    	return temp;
    }
    };

Log in to reply
 

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