class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode * ans,*pre,*next,*temp;
if(head==NULL || head->next==NULL) return head;
ans=head->next;
pre=head;
next=head->next;
while(true)
{
pre->next=pre->next->next;
next->next=pre;
temp=pre->next;
if(temp==NULL || temp->next==NULL) break;
pre->next=pre->next->next;
pre=temp;
next=pre->next;
}
return ans;
}
};
相关题目链接:https://leetcode.com/problems/swap-nodes-in-pairs/
leetcode_swap node in pairs
最新推荐文章于 2022-03-21 23:30:51 发布