/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* swapPairs(struct ListNode* head) {
if(head == NULL)
return NULL;
struct ListNode* cur = head;
struct ListNode* prev = cur;
while(cur)
{
if(cur == head)
{
struct ListNode* next1 = cur->next;
if(next1 == NULL)
return head;
prev->next = next1->next;
next1->next = prev;
head = next1;
cur = next1->next;
prev = cur;
cur = cur->next;
}
else
{
struct ListNode* next1 = cur->next;
if(next1 == NULL)
return head;
struct ListNode* next2 = next1->next;
prev->next = next1;
cur->next = next2;
next1->next = cur;
prev = cur;
if(cur == NULL)
break;
cur = cur->next;
}
}
return head;
}