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         if (!head || !head->next) return head;
13         ListNode *result = new ListNode(0);
14         result->next = head;
15         head = result;
16         while (head->next && head->next->next) {
17             ListNode *current = head->next->next;
18             head->next->next = current->next;
19             current->next = head->next;
20             head->next = current;
21             head = head->next->next;
22         }
23         return result->next;
24     }
25 };

 

posted on 2015-03-24 16:49  keepshuatishuati  阅读(124)  评论(0编辑  收藏  举报