Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5db7fa4 commit 4bffdabCopy full SHA for 4bffdab
algorithms/cpp/swapNodesInPairs/swapNodesInPairs.cpp
@@ -71,4 +71,23 @@ class Solution {
71
72
return head;
73
}
74
+
75
+ ListNode* swapPairs3(ListNode* head) {
76
+ // Three pointers point current, previous and next node.
77
+ ListNode *Curr=head, *Prev=NULL, *Next=NULL;
78
79
+ while (Curr && Curr->next ) {
80
+ Next = Curr->next;
81
82
+ //swap nodes
83
+ Curr->next = Next->next;
84
+ Prev == NULL ? head = Prev = Next : Prev->next = Next;
85
+ Next->next = Curr;
86
87
+ //set the pointers to next place.
88
+ Prev = Curr;
89
+ Curr = Curr->next;
90
+ }
91
+ return head;
92
93
};
0 commit comments