当前位置:网站首页>PAT 乙等 1025 反转链表
PAT 乙等 1025 反转链表
2022-06-23 04:11:00 【章鱼bro】
1025. 反转链表 (25)
给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4;如果K为4,则输出应该为4→3→2→1→5→6,即最后不到K个元素不反转。
输入格式:
每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数正整数N(<= 105)、以及正整数K(<=N),即要求反转的子链结点的个数。结点的地址是5位非负整数,NULL地址用-1表示。
接下来有N行,每行格式为:
Address Data Next
其中Address是结点地址,Data是该结点保存的整数数据,Next是下一结点的地址。
输出格式:
对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。
输入样例:00100 6 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 2 33218输出样例:
00000 4 33218 33218 3 12309 12309 2 00100 00100 1 99999 99999 5 68237 68237 6 -1
思路:本题操作的数据元素是一个包含地址、数据、下一个节点地址的结构体,先申请一个100001的结构体的数组,采用动态分配内存,直接声明数组在dev中会溢出。将数组下标作为地址,每输入一个节点就找到地址对应的下标位置存进去。在存储好所有结点以后,按照链表的顺序(即从第一个节点的地址开始寻找,直到遇到-1结束)排序之后存入另一个数组,最后在排好序的数组中将K个元素倒序。 注意,答主在最后一个测试点卡了long long time!! 最后发现该测试用例是含有不在链表中的节点~,~
所以要在遇到next为-1时就退出输入。
一、起始变量
1.input[100001],用来保存输入的节点
2.sortQ[100001],用来保存按照链表序列排好的序列
3.add链表中第一个节点的地址
4.N节点个数
5.K子序列个数
二、算法
1.输入所有节点
2.将input中的节点按照地址排好序放入sortQ序列中;
3.将子序列里的节点进行反转
4.修改最后逆序后的每个节点的next并输出
三、代码
#include "stdio.h"
#include "stdlib.h"
typedef struct{
int address;//地址
int data;//数据
int next;//下一个节点地址
}node;
int main()
{
node * input = (node *)malloc(sizeof(node) * 100001);//用来存储输入的节点
node * sortQ = (node *)malloc(sizeof(node) * 100001);//用来存储排好序的节点
int add,N,K;
scanf("%d %d %d",&add, &N, &K);//输入起始变量
//1.输入节点
for(int i = 0; i < N; i++)
{
node tmp;
scanf("%d %d %d",&tmp.address, &tmp.data, &tmp.next);
input[tmp.address] = tmp;
}
//2.将input中的节点按照地址排好序放入sortQ序列中;
for(int i = 0; i < N; i++)
{
sortQ[i] = input[add];
add = input[add].next;
//最后一个测试点在加上这个if以后通过,即只要遇到结尾即更改N跳出循环
//若不加则会访问到没有存入数据的节点,在之后的倒序就会出现错误
if(add == -1)
{
N = i + 1;
break;
}
}
int converseNum = N / K;//要反转的子序列数目
//3.将子序列里的节点进行反转
for(int i = 0; i < converseNum; i++)
{
for(int j = 0; j < K / 2; j++)
{
node tmp = {0, 0, 0};
tmp = sortQ[i * K + j];//第i(从0开始)个子序列中的第j个与倒数第j个交换
sortQ[i * K + j] = sortQ[i * K + K - 1 - j];
sortQ[i * K + K - 1 - j] = tmp;
}
}
//4.此时sortQ中的节点已经是反转后的序列,只需修改每个节点的next
int i = 0;
for(i = 0; i < N - 1; i++)//修改并输出前n-1个节点
{
sortQ[i].next = sortQ[i + 1].address;
printf("%05d %d %05d\n",sortQ[i].address,sortQ[i].data,sortQ[i].next);
}
sortQ[i].next = -1;//最后一个节点的next为-1
printf("%05d %d %d\n",sortQ[i].address,sortQ[i].data,sortQ[i].next);
return 0;
}
边栏推荐
- SIFT feature point extraction
- What does the English letter PC mean? What does the Internet PC mean
- Win11应用商店一直转圈解决办法
- Database connection exception: create connection error, url: jdbc: mysql://ip/ Database name, errorcode 0, state 08s01 problem handling
- 高等数学(第七版)同济大学 习题1-7 个人解答
- jvm: 方法重载时,具体调用哪个方法,是由传入参数的静态类型来决定的,而不是由参数的实际类型来决定
- AI艺术的基因工程?使用 #Artbreeder 改变图像的任意形态
- Redis cache penetration solution - bloom filter
- How does win11 enable mobile hotspot? How to enable mobile hotspot in win11
- 数字藏品到底有什么魔力?目前有哪些靠谱的团队在开发
猜你喜欢

数字藏品到底有什么魔力?目前有哪些靠谱的团队在开发

Leetcode 797: all possible paths

【斯坦福计网CS144项目】Lab2: TCPReceiver

MySQL面试真题(二十八)——案例-通讯运营商指标分析

Wechat applet: an artifact for calculating the full amount of orders

Fs2119a Synchronous Boost IC output 3.3V and fs2119b Synchronous Boost IC output 5V

SIFT feature point extraction

Wechat applet: elderly blessing short video
![[opencv450] inter frame difference method](/img/ad/c8a56e27d78cea581deb1874620613.png)
[opencv450] inter frame difference method

FS4059A与FS5080E充电芯片的区别
随机推荐
Common wireless charging and transmitting IC chips
Use of visdom
What if win11 cannot record audio? Solution of win11 unable to input sound
FS4059A与FS5080E充电芯片的区别
Real MySQL interview question (30) -- shell real estate order analysis
Meteorological mapping software panoply tutorial (updated from time to time)
Opencv display image
fastjson中的@JSONField注解
LeetCode-1757. Recyclable and low-fat products_ SQL
数字藏品赋能实体产业释放了哪些利好?
MDM数据清洗功能开发说明
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-8 personal solutions
STC 32比特8051單片機開發實例教程 一 開發環境搭建
手机无线充电双线圈15W方案SOC英集芯IP6809
IP6809三线圈15W无线充电发射端方案ic英集芯
Current situation and development of containerization technology under the cloud native trend
ssm项目搭建
Software design and Development Notes 2: serial port debugging tool based on QT design
What does the English letter PC mean? What does the Internet PC mean
英集芯IP5566带TYPE-C口3A充放快充移动电源5w无线充二合一方案SOC