diff --git a/algorithms/cpp/swapNodesInPairs/swapNodesInPairs.cpp b/algorithms/cpp/swapNodesInPairs/swapNodesInPairs.cpp index b5d03dd04..9f90d7c0c 100644 --- a/algorithms/cpp/swapNodesInPairs/swapNodesInPairs.cpp +++ b/algorithms/cpp/swapNodesInPairs/swapNodesInPairs.cpp @@ -71,4 +71,23 @@ class Solution { return head; } + + ListNode* swapPairs3(ListNode* head) { + // Three pointers point current, previous and next node. + ListNode *Curr=head, *Prev=NULL, *Next=NULL; + + while (Curr && Curr->next ) { + Next = Curr->next; + + //swap nodes + Curr->next = Next->next; + Prev == NULL ? head = Prev = Next : Prev->next = Next; + Next->next = Curr; + + //set the pointers to next place. + Prev = Curr; + Curr = Curr->next; + } + return head; + } };