AcWing 3709. 单链表节点交换
原题链接
简单
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int N = 1e6+10;
int t,n,m,k,l,r,op,x,y;
int f[N];
bool flag;
struct node{
int v;
node* next;
node():v(0),next(nullptr){}
node(int _v):v(_v),next(nullptr){};
};
void solve(){
cin>>n;
node* dummy = new node();
node* cur = dummy;
for(int i = 1;i<=n;i++){
cin>>x;
cur->next = new node(x);
cur = cur->next;
}
// cur = dummy->next;
// while(cur){
// cout<<(cur->v)<<" ";
// cur = cur->next;
// }
cur = dummy;
node* t;
while((n%2)?cur->next->next:cur->next){
t = cur->next;
cur->next = t->next;
cur = cur->next;
t->next = cur->next;
cur->next = t;
cur = cur->next;
}
cur = dummy->next;
while(cur){
cout<<(cur->v)<<" ";
cur = cur->next;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}