#include "iostream"
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
void print(ListNode *head)
{
ListNode *p = head;
while (p != NULL)
{
cout << p->val << " ";
p = p->next;
}
cout << endl;
}
ListNode *swapPairs(ListNode *head)
{
if (head == NULL)
{
return NULL;
}
else if (head->next == NULL)
{
return head;
}
ListNode *node = new ListNode(0);
node->next = head;
ListNode *p = head, *s = head->next;
ListNode *f=node;
// 1 2 3 4
while (true)
{
p->next = s->next;
s->next = p;
f->next = s;
f = p;
p = p->next;
if(p==NULL)
{
break;
}
s = p->next;
if(s==NULL)
{
break;
}
}
return node->next;
}
};
int main(int argc, char const *argv[])
{
int x;
ListNode *p, *head;
cin >> x;
head = new ListNode(x);
p = head;
while (true)
{
cin >> x;
p->next = new ListNode(x);
p = p->next;
if (cin.get() == '\n')
break;
}
Solution so;
head = so.swapPairs(head);
return 0;
}