/** * 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* res = head; ListNode* term = res; while(head!=NULL) { if(head->next!=NULL) { ListNode* temp = head->next; head->next = temp->next; temp->next = head; term->next = temp; term=head; head=head->next; } else break; } return res; }};