24. Swap Nodes in Pairs
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
ListNode* dummy=new ListNode(0);
ListNode* cur=dummy;
dummy->next=head;
while(cur->next!=NULL && cur->next->next!=NULL){
ListNode* first=cur->next;
ListNode* second=cur->next->next;
first->next=second->next;
cur->next=second;
cur->next->next=first;
cur=cur->next->next;
}
return dummy->next;
}
};