//思路一
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head)
{
if(head == NULL || head->next == NULL)
return head;
ListNode* dummy = new ListNode(-1);
ListNode* pre = dummy;
dummy->next = head;
while(pre->next && pre->next->next)
{
ListNode* current = pre->next->next;
pre->next->next = current->next;
current->next = pre->next;
pre->next = current;
pre = current->next;
}
return dummy->next;
}
};
class Solution {
public:
ListNode* swapPairs(ListNode* head)
{
if(head == NULL || head->next == NULL)
return head;
ListNode* temp = head->next;
head->next = swapPairs(temp->next);
temp->next = head;
return temp;
}
};