7-2 Reversing Linked List

題目

題意:給定鏈表上的每個節點信息,要求連續K的結點翻轉,並輸出最後翻轉後的結果 

tip:模擬+reverse

#include<iostream>
#include<algorithm>
using namespace std;
int main() {
	int st,n,m;
	cin>>st>>n>>m;
	int data[100000],next[100000],list[100000];
	for(int i=0; i<n; ++i) {
		int temp;
		cin>>temp;
		cin>>data[temp]>>next[temp];
	}
	int count=0;
	while(st!=-1) {//不一定是所有的數據都是一條鏈上的 
		list[count++]=st;
		st=next[st];
	}
	for(int i=0; i<count-count%m; i+=m)
		reverse(list+i,list+i+m);
	for(int i=0; i<count-1; ++i)
		printf("%05d %d %05d\n",list[i],data[list[i]],list[i+1]);
	printf("%05d %d -1",list[count-1],data[list[count-1]]);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章