From 90d0c5975a391740090f039ccf02dd456abbf212 Mon Sep 17 00:00:00 2001 From: Aryaman Garg <166644611+aryaman0406@users.noreply.github.com> Date: Thu, 30 Oct 2025 17:43:09 +0530 Subject: [PATCH] Implement swapPairs function to swap nodes --- Swap Nodes in Paris | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Swap Nodes in Paris 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; + } +};