标签:
1 class Solution { 2 public: 3 ListNode* swapPairs(ListNode* head) { 4 if (head == nullptr || head->next == nullptr) return head; 5 else { 6 ListNode* curr = head; // 当前节点 7 head = head->next; // 返回值 8 ListNode* prev = new ListNode(-1); // 用于连接前一个翻转后的第二个节点和后一个反转后的第一个ie节点 9 prev->next = curr; 10 ListNode* temp = prev; 11 while (curr != nullptr && curr->next != nullptr) { 12 ListNode* first = curr; 13 ListNode* second = curr->next; 14 curr = second->next; 15 first->next = curr; 16 second->next = first; 17 prev->next = second; 18 prev = prev->next->next; 19 } 20 delete temp; 21 return head; 22 } 23 } 24 };
标签:
原文地址:http://www.cnblogs.com/shadowwalker9/p/5751484.html