PAT 乙级1025/甲级 1074 反转链表

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.

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

#### 解题思路:本题先找出在链中的所有节点,之后通过reverse函数把每一个小块的数据反转。

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
	int address;
	int data;
	int next;
}a[100000],b[100000];
int main(void)
{
	int start,N,K,address,sum = 0;
	scanf("%d %d %d",&start,&N,&K);
	for(int i = 0;i < N;i++)
	{
		scanf("%d",&address);
		a[address].address = address;
		scanf("%d %d",&a[address].data,&a[address].next);
	}
	for(int i = start;i!=-1;i = a[i].next)
		b[sum++] = a[i];
	for(int i = 0;i < sum - sum % K;i+=K)
		reverse(b+i,b+i+K);
	for(int i = 0;i < sum - 1;i++)
	{
		printf("%05d %d %05d\n",b[i].address,b[i].data,b[i+1].address);
	}
	printf("%05d %d -1",b[sum - 1].address,b[sum - 1].data);
}

一开始没有考虑到用reverse函数实现反转,通过直接输出的方式写的比较复杂

#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
	int address;
	int data;
	int next;
	int flag;
}a[100000],b[100000];
int main(void)
{
	int start,N,K,address,sum = 0,num;
	scanf("%d %d %d",&start,&N,&K);
	num = N;
	while(num--)
	{
		scanf("%d",&address);
		a[address].address = address;
		scanf("%d %d",&a[address].data,&a[address].next);
	}
	for(int i = start;i!=-1;i = a[i].next)
	{
		b[sum++] = a[i];
	}
	int remain = sum % K;
	int cir = sum / K;
	int temp;
	for(int i = 1;i <= cir;i++)
	{
		temp = K*i;
		if(i!=cir)
		{
			for(int j = 1;j <= K;j++)
			{	
				if(j == K)
					printf("%05d %d %05d\n",b[temp - j].address,b[temp - j].data,b[temp + K - 1].address);
				else
					printf("%05d %d %05d\n",b[temp - j].address,b[temp - j].data,b[temp - j - 1].address);
			}
		}
		else
		{
			for(int j = 1;j <= K;j++)
			{	
				if(j < K)
					printf("%05d %d %05d\n",b[temp - j].address,b[temp - j].data,b[temp - j - 1].address);
				else if(j == K&&remain == 0)
					printf("%05d %d -1\n",b[temp - j].address,b[temp - j].data);
				else
				{
					printf("%05d %d %05d\n",b[temp - j].address,b[temp - j].data,b[temp].address);
					for(int i = temp;i < sum;i++)
					{
						if(i != sum - 1)
							printf("%05d %d %05d\n",b[i].address,b[i].data,b[i].next);
						else
							printf("%05d %d -1",b[i].address,b[i].data);
					}
				}
			}
		}
	}
	if(cir == 0) 
	{
		for(int i = 0;i < sum;i++)
		{
			if(i!=sum - 1)
				printf("%05d %d %05d\n",b[i].address,b[i].data,b[i+1].address);
			else
				printf("%05d %d -1\n",b[i].address,b[i].data);
		}
	}
	return 0;
}
发布了24 篇原创文章 · 获赞 1 · 访问量 502

猜你喜欢

转载自blog.csdn.net/lovingcgq/article/details/104395332