From 1c8fc61e361ab7ed71619a825bdeeef051d561eb Mon Sep 17 00:00:00 2001 From: AbhishekSingh581 <91012436+AbhishekSingh581@users.noreply.github.com> Date: Mon, 17 Oct 2022 20:30:45 +0530 Subject: [PATCH] Swap_Nodes_in_Pairs.cpp --- 18_Swap_Nodes_in_Pairs.cpp | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 18_Swap_Nodes_in_Pairs.cpp diff --git a/18_Swap_Nodes_in_Pairs.cpp b/18_Swap_Nodes_in_Pairs.cpp new file mode 100644 index 00000000..0db29851 --- /dev/null +++ b/18_Swap_Nodes_in_Pairs.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; +using lli = long long int; +using ll = long long; +#define endl '\n' + +struct ListNode +{ + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +class Solution +{ +public: + ListNode* swapPairs(ListNode* head) { + ListNode* p=head; + ListNode* k=new ListNode(0); + ListNode* prev=k; + prev->next=head; + while(p!=NULL && p->next!=NULL){ + ListNode* q=p->next; + prev->next=q; + p->next=q->next; + q->next=p; + prev=p; + p=p->next; + } + return k->next; + } +}; + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(0); + + return 0; +} \ No newline at end of file