Given a singly linked list L. Let us consider every K nodes as a block (if there are less than K nodes at the end of the list, the rest of the nodes are still considered as a block). Your job is to reverse all the blocks in L. For example, given L as 1→2→3→4→5→6→7→8 and K as 3, your output must be 7→8→4→5→6→1→2→3.
注:可能并不是每个点都在链表上
自己记个数
#include<bits/stdc++.h>
using namespace std;
int a[100000][2];
struct node{
int add;
int p;
int next;
}p[100010],ppp[100010];
int main()
{
int f,n,k,add,data,next;
cin>>f>>n>>k;
for(int i=0;i<n;i++){
cin>>add>>data>>next;
a[add][0]=data;
a[add][1]=next;
}
int cnt=f,flag=0,xxx=0;
while(cnt!=-1){
// cout<<cnt<<endl;
p[flag].add=cnt;
p[flag].p=a[cnt][0];
p[flag].next=a[cnt][1];
cnt=a[cnt][1];
flag++;
}
n=flag;
cnt=n;
while(cnt!=0){
if(cnt%k!=0){
flag=cnt%k;
}else{
flag=k;
}
for(int i=0;i<flag;i++){
ppp[xxx].add=p[cnt-flag+i].add;
ppp[xxx].p=p[cnt-flag+i].p;
ppp[xxx].next=-1;
if(xxx!=0){
ppp[xxx-1].next=ppp[xxx].add;
}
xxx++;
//cout<<p[cnt-flag+i].add<<" "<<p[cnt-flag+i].p<<" "<<p[cnt-flag+i].next<<endl;
}
cnt-=flag;
}
for(int i=0;i<n-1;i++){
printf("%05d %d %05d\n",ppp[i].add,ppp[i].p,ppp[i].next);
}
printf("%05d %d -1\n",ppp[n-1].add,ppp[n-1].p);
return 0;
}