Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions 18_Swap_Nodes_in_Pairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <bits/stdc++.h>
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;
}