class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode new_head(0);
new_head.next = head;
ListNode* iter = &new_head;
while (iter->next && iter->next->next) {
ListNode *tmp1 = iter->next, *tmp2 = iter->next->next->next;
iter->next = iter->next->next;
iter->next->next = tmp1;
tmp1->next = tmp2;
iter = tmp1;
}
return new_head.next;
}
};
Swap Nodes in Pairs 11行AC
最新推荐文章于 2024-08-27 09:16:55 发布