1 /**
2 * Definition for singly-linked list.
3 * struct ListNode {
4 * int val;
5 * ListNode *next;
6 * ListNode(int x) : val(x), next(NULL) {}
7 * };
8 */
9 class Solution {
10 public:
11 ListNode *swapPairs(ListNode *head) {
12 // Note: The Solution object is instantiated only once and is reused by each test case.
13 ListNode * last = NULL;
14 ListNode * ite = head;
15 ListNode * rlt = head;
16 while ((ite != NULL) && (ite->next != NULL)){
17 ListNode * a = ite;
18 ListNode * b = ite->next;
19 a->next = b->next;
20 b->next = a;
21 if (last != NULL){
22 last->next = b;
23 }
24 else {
25 rlt = b;
26 }
27 last = a;
28 ite = last->next;
29 }
30 return rlt;
31 }
32 };