1074 Reversing Linked List (25分)(链表区间反转)

1074 Reversing Linked List (25分)

 

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 (≤) which is the total number of nodes, and a positive K (≤) 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.

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.

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


题目说给一个单链,好家伙,最后一个样例里面有些节点不存在于这条单链上,真的一点都不严谨啊。
就好像题目说给你颗二叉树,结果最后整个图出现多个连通块一样。吐吐的。

 1 #include <cstdio>
 2 using namespace std;
 3 
 4 const int maxn = 1e6 + 5, maxm = 1e5 + 5;
 5 
 6 struct Linked {
 7     int pre, next, data;
 8 } lists[maxn];
 9 int val[maxm], cnt;
10 
11 int main() {
12     int head, n, k, u, v, data;
13     scanf("%d %d %d", &head, &n, &k);
14     for(int i = 0; i < n; i ++) {
15         scanf("%d %d %d", &u, &data, &v);
16         lists[u].next = v;
17         lists[v].pre = u;
18         lists[u].data = data;
19         if(u == head) lists[u].pre = -1;
20     }
21     while(~head) {
22         Linked temp = lists[head];
23         val[++ cnt] = head;
24         head = temp.next;
25     }
26     if(cnt >= k) {
27         for(int i = 1; i <= cnt / k; i ++) {
28             for(int j = i * k; j > (i - 1) * k + 1; j --) {
29                 Linked temp = lists[val[j]];
30                 printf("%05d %d %05d\n", val[j], temp.data,temp.pre);
31             }
32             Linked temp = lists[val[k * (i - 1) + 1]];
33             if(i < cnt / k) printf("%05d %d %05d\n", val[k * (i - 1) + 1], temp.data, val[k * (i + 1)]);
34             else {
35                 if(cnt % k) {
36                     printf("%05d %d %05d\n", val[k * (i - 1) + 1], temp.data, val[k * i + 1]);
37                     int flag = (cnt / k) * k;
38                     for(int j = 1; j < cnt % k; j ++) {
39                         Linked temp = lists[val[flag + j]];
40                         printf("%05d %d %05d\n", val[flag + j], temp.data, temp.next);
41                     }
42                     printf("%05d %d -1\n", val[flag + cnt % k], lists[val[flag + cnt % k]].data);
43                 } else {
44                     printf("%05d %d -1\n", val[k * (i - 1) + 1], temp.data);
45                 }
46             }
47         }
48     } else {
49         for(int i = 1; i < cnt; i ++) {
50             printf("%05d %d %05d\n", val[i], lists[val[i]].data, lists[val[i]].next);
51         }
52         printf("%05d %d -1\n", val[cnt], lists[val[cnt]].data);
53     }
54     return 0;
55 }

 




posted @ 2020-06-12 23:28  Cruel_King  阅读(207)  评论(0编辑  收藏  举报