两种方法:
1.用递归来反转链表
#include<cstdio>
#define maxn 100010
int n,k;
struct Node{
	int data,next;
}node[maxn];
int reverse(int head){
	if(node[head].next==-1)return head;
	int p=node[head].next,h,t;
	h=reverse(p);t=h;
	while(node[t].next!=-1)t=node[t].next;
	node[t].next=head;
	node[head].next=-1;
	return h;
}
int main(){
	//freopen("A1074.txt","r",stdin);
	int head;
	scanf("%d%d%d",&head,&n,&k);
	while(n--){
		int temp;
		scanf("%d",&temp);
		scanf("%d%d",&node[temp].data,&node[temp].next);
	}
	int p=head,h=-1,t=-1;
	while(p!=-1){
		p=node[p].next;
		n++;
	}
	n++;
	h=head;
	while(n>=k){
		p=h;
		for(int i=1;i<k;i++){
			p=node[p].next;
		}
		h=node[p].next;
		node[p].next=-1;
		if(t==-1){
			head=reverse(head);
			p=head;
		}
		else{
			node[t].next=reverse(node[t].next);
	    	p=node[t].next;
		}
		while(node[p].next!=-1)p=node[p].next;
		t=p;
		node[t].next=h;
		n=n-k;
	}
	while(head!=-1){
		printf("%05d %d ",head,node[head].data);
		if(node[head].next==-1)printf("-1");
		else printf("%05d\n",node[head].next);
		head=node[head].next;
	}
	return 0;
}
2.用排序来实现
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 100010
int n=0,k;
struct Node{
	int id,data,next,order=maxn;
}node[maxn];
bool cmp(Node a,Node b){
	return a.order<b.order;
}
bool cmp_reverse(Node a,Node b){
	return a.order>b.order;
}
int main(){
	//freopen("A1074.txt","r",stdin);
	int N,head;
	scanf("%d%d%d",&head,&N,&k);
	while(N--){
		int temp;
		scanf("%d",&temp);
		scanf("%d%d",&node[temp].data,&node[temp].next);
		node[temp].id=temp;
	}
	int p=head;
	while(p!=-1){
		node[p].order=n+1;
		p=node[p].next;
		n++;
	}
	sort(node,node+maxn,cmp);
	int r=n,i=0;
	while(r>=k){
		sort(node+i*k,node+i*k+k,cmp_reverse);
		r=r-k;
		i++;
	}
	for(int i=0;i<n;i++){
		printf("%05d %d ",node[i].id,node[i].data);
		if(i<n-1)printf("%05d\n",node[i+1].id);
		else printf("-1");
	}
	return 0;
}