//2014年8月25日22:57:33
//2014年8月25日23:11:30
//水题 链表操作
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
if(head==NULL || head->next==NULL){
return head;
}
ListNode fakeHead(0);
fakeHead.next = head;
ListNode *prior = &fakeHead;
ListNode *cur = head;
while(cur!=NULL && cur->next!=NULL){
prior->next = cur->next;
cur->next = cur->next->next;
prior->next->next = cur;
prior = prior->next->next;
cur = cur->next;
}
return fakeHead.next;
}
};
int main()
{
ListNode node1(1);
ListNode node2(3);
ListNode node3(5);
node1.next = &node2;
node2.next = &node3;
Solution S;
ListNode *ret = S.swapPairs(&node1);
while(ret != NULL){
cout << ret->val << " ";
ret = ret -> next;
}
cout << endl;
return 0;
}