/**
* 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) {
ListNode *pre=NULL;
ListNode *first=NULL;
ListNode *second=head;
int k=1;
while(second)
{
if(k%2==0)
{
first->next=second->next;
second->next=first;
if(pre==NULL)
head=second;
else
pre->next=second;
ListNode *temp=first;
first=second;
second=temp;
}
pre=first;
first=second;
second=second->next;
k++;
}
return head;
}
};
* 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) {
ListNode *pre=NULL;
ListNode *first=NULL;
ListNode *second=head;
int k=1;
while(second)
{
if(k%2==0)
{
first->next=second->next;
second->next=first;
if(pre==NULL)
head=second;
else
pre->next=second;
ListNode *temp=first;
first=second;
second=temp;
}
pre=first;
first=second;
second=second->next;
k++;
}
return head;
}
};