ListNode *swapPairs(ListNode *head) {
if (head == nullptr || head->next == nullptr)
return head;
ListNode *it = head;
head = head->next;
it->next = head->next;
head->next = it;
while (it->next != nullptr && it->next->next != nullptr) {
ListNode *temp = it->next;
it->next = it->next->next;
temp->next = it->next->next;
it->next->next = temp;
it = it->next->next;
}
return head;
}
原文地址:https://www.cnblogs.com/INnoVationv2/p/10173091.html
时间: 2024-10-16 00:03:47