#include <cstdio>
#include <vector>
#include <algorithm>
const int MAXN = 100001;
struct node{
int data;
int next;
};
int curr, N, K, address;
node vec[MAXN];
std::vector<int> ans;
int main(){
scanf("%d %d %d", &curr, &N, &K);
for(int i = 0; i < N; ++i){
scanf("%d", &address);
scanf("%d %d", &vec[address].data, &vec[address].next);
}
while(curr != -1){
ans.push_back(curr);
curr = vec[curr].next;
}
for(int i = 0; i < ans.size() / K; ++i){
reverse(ans.begin() + i * K, ans.begin() + (i + 1) * K);
}
for(int i = 0; i < ans.size() - 1; ++i){
printf("%05d %d %05d\n", ans[i], vec[ans[i]].data, ans[i + 1]);
}
printf("%05d %d -1\n", ans.back(), vec[ans.back()].data);
return 0;
}
题目如下:
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address
is the position of the node, Data
is an integer, and Next
is the position of the next node.文章来源:https://www.toymoban.com/news/detail-486670.html
Output Specification:
For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.文章来源地址https://www.toymoban.com/news/detail-486670.html
Sample Input:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
Sample Output:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
到了这里,关于1074 Reversing Linked List (PAT甲级)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!