给定一个单链表 L,我们将每 K 个结点看成一个区块(链表最后若不足 K 个结点,也看成一个区块),请编写程序将 L 中所有区块的链接反转。例如:给定 L 为 1→2→3→4→5→6→7→8,K 为 3,则输出应该为 7→8→4→5→6→1→2→3。
输入格式:
每个输入包含 1 个测试用例。每个测试用例第 1 行给出第 1 个结点的地址、结点总个数正整数 N (≤105)、以及正整数 K (≤N),即区块的大小。结点的地址是 5 位非负整数,NULL 地址用 −1 表示。
接下来有 N 行,每行格式为:
Address Data Next
其中 Address
是结点地址,Data
是该结点保存的整数数据,Next
是下一结点的地址。
输出格式:
对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。
输入样例:
00100 8 3
71120 7 88666
00000 4 99999
00100 1 12309
68237 6 71120
33218 3 00000
99999 5 68237
88666 8 -1
12309 2 33218
输出样例:
71120 7 88666
88666 8 00000
00000 4 99999
99999 5 68237
68237 6 00100
00100 1 12309
12309 2 33218
33218 3 -1
C:
#include <stdio.h>
int w[100010], ne[100010], q[100010];
int main() {
int h, n, m, address, data, next;
scanf("%d %d %d", &h, &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &address, &data, &next);
w[address] = data;
ne[address] = next;
}
int cnt = 0;
for (int i = h; i != -1; i = ne[i]) {
q[cnt++] = i;
}
reverse(q, q + cnt);
for (int i = cnt - 1; i >= 0; i = i - m) {
reverse(q + max(0, i + 1 - m), q + i + 1);
}
for (int i = 0; i < cnt; i++) {
printf("%05d %d ", q[i], w[q[i]]);
if (i == cnt - 1) {
printf("-1\n");
} else {
printf("%05d\n", q[i + 1]);
}
}
return 0;
}
C++:
#include <bits/stdc++.h>
using namespace std;
int w[100010],ne[100010],q[100010];
int main()
{
int h,n,m,address,data,next;
scanf("%d %d %d",&h,&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d %d %d",&address,&data,&next);
w[address]=data;
ne[address]=next;
}
int cnt=0;
for(int i=h;i!=-1;i=ne[i])
{
q[cnt++]=i;
}
reverse(q,q+cnt);
for(int i=cnt-1;i>=0;i=i-m)
{
reverse(q+max(0,i+1-m),q+i+1);
}
for(int i=0;i<cnt;i++)
{
printf("%05d %d ",q[i],w[q[i]]);
if(i==cnt-1)
{
printf("-1\n");
}
else
{
printf("%05d\n",q[i+1]);
}
}
return 0;
}
Java:
import java.util.Scanner;
public class Main {
static int[] w = new int[100010];
static int[] ne = new int[100010];
static int[] q = new int[100010];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int h = scanner.nextInt();
int n = scanner.nextInt();
int m = scanner.nextInt();
for (int i = 0; i < n; i++) {
int address = scanner.nextInt();
int data = scanner.nextInt();
int next = scanner.nextInt();
w[address] = data;
ne[address] = next;
}
int cnt = 0;
for (int i = h; i != -1; i = ne[i]) {
q[cnt++] = i;
}
for (int i = cnt - 1; i >= 0; i -= m) {
for (int j = Math.max(0, i + 1 - m); j < i + 1 && j < cnt; j++) {
if (j > i) {
break;
}
for (int k = j; k < (j + i + 1) / 2; k++) {
int temp = q[k];
q[k] = q[i - j + k];
q[i - j + k] = temp;
}
}
}
for (int i = 0; i < cnt; i++) {
System.out.printf("%05d %d ", q[i], w[q[i]]);
if (i == cnt - 1) {
System.out.print("-1\n");
} else {
System.out.printf("%05d\n", q[i + 1]);
}
}
}
}
Python:
w = [0] * 100010
ne = [0] * 100010
q = [0] * 100010
h, n, m = map(int, input().split())
for _ in range(n):
address, data, next = map(int, input().split())
w[address] = data
ne[address] = next
cnt = 0
i = h
while i != -1:
q[cnt] = i
cnt += 1
i = ne[i]
q[:cnt] = q[:cnt][::-1]
for i in range(0, cnt, m):
if i + m <= cnt:
q[i:i+m] = q[i:i+m][::-1]
else:
q[i:] = q[i:][::-1]
for i in range(cnt):
print(f"{q[i]:05d} {w[q[i]]} ", end="")
if i == cnt - 1:
print("-1")
else:
print(f"{q[i+1]:05d}")