标签:
题目链接:https://leetcode.com/problems/swap-nodes-in-pairs/
/*题意:将链表相邻的两两结点交换*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == NULL || head->next == NULL) return head;
ListNode *pre = NULL;
ListNode *Next = head;
while(Next && Next->next) {
ListNode *first = Next;
ListNode *second = Next->next;
Next = second->next;
if(pre) {
pre->next = second;
second->next = first;
first->next = Next;
pre = first;
}
else {
head = second;
second->next = first;
first->next = Next;
pre = first;
}
}
return head;
}
};
标签:
原文地址:http://www.cnblogs.com/jzmzy/p/4517646.html