标签:ble https tco href 返回 else list 一个 swap
地址:https://leetcode-cn.com/problems/swap-nodes-in-pairs/
大意:给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == NULL || head->next == NULL)
return head;
ListNode *hh = new ListNode(0);
hh->next = head;
ListNode *one = hh;
ListNode *two = hh;
while(one != NULL && one->next != NULL){
two = one->next;
if(two->next != NULL){
ListNode *second = two->next;
two->next = two->next->next;
second->next = one->next;
one->next = second;
}else
break;
one = one->next->next;
}
return hh->next;
}
};
标签:ble https tco href 返回 else list 一个 swap
原文地址:https://www.cnblogs.com/c21w/p/12683532.html