diff --git a/Swap Nodes in Paris b/Swap Nodes in Paris new file mode 100644 index 0000000..0b9f7e5 --- /dev/null +++ b/Swap Nodes in Paris @@ -0,0 +1,21 @@ +class Solution { +public: + ListNode* swapPairs(ListNode* head) { + ListNode dummy(0, head); + ListNode *prev = &dummy, *cur = head; + + while (cur && cur->next) { + ListNode *npn = cur->next->next; + ListNode *second = cur->next; + + second->next = cur; + cur->next = npn; + prev->next = second; + + prev = cur; + cur = npn; + } + + return dummy.next; + } +};