PAT Advanced 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 (<= 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
题目分析
已知N个结点,以K为步长,反转链表
解题思路
- 定义结点,使用数组存储N个结点,使用order属性记录链表中结点出现序号
- 将链表分为n/k个段,对每个段进行反转
2.1 每个节点的next即为反转后的下一个结点地址
2.2 每个段最后一个节点的特殊处理:next为下一个段反转前最后一个结点
2.3 最后一个段的处理- 若n%k==0,说明n可以正好分为n/k个段,最后一个段反转,并将最后一个节点置为-1
- 若n%k!=0,说明最后一段小于k,开始下标为n/k*k,不需要反转,顺序打印,并将最后一个结点置为-1
知识点
- 将n个结点分为n/k个段,最后一段开始下标为n/k*k(下标从0开始)
- 链表反转,不一定非要对链表进行反转(即:更换每个结点next值),可以使用本题思想反向打印的办法
Code
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100010;
struct node {
int adr;
int data;
int next;
bool flag=false;//初始化为false
int order=maxn;
} nds[maxn];
bool cmp(node &n1,node &n2) {
return n1.order<n2.order;
}
int main(int argc,char *argv[]) {
int hadr,n,k,adr;
scanf("%d %d %d",&hadr,&n,&k);
for(int i=0; i<n; i++) {
scanf("%d",&adr);
scanf("%d %d",&nds[adr].data,&nds[adr].next);
nds[adr].adr=adr;
}
int count=0;
for(int i=hadr; i!=-1; i=nds[i].next) {
// nds[i].flag=true;
nds[i].order=count++;
}
sort(nds,nds+maxn,cmp);
n=count;
// for(int i=0; i<n; i++) {
// printf("%05d %05d %05d\n",nds[i].adr,nds[i].data,nds[i].next);
// }
for(int i=0; i<n/k; i++) {
for(int j=(i+1)*k-1; j>i*k; j--) {
printf("%05d %d %05d\n",nds[j].adr,nds[j].data,nds[j-1].adr);
}
printf("%05d %d",nds[i*k].adr,nds[i*k].data);
if(i<n/k-1) {
printf(" %05d\n",nds[(i+2)*k-1].adr);
} else {
if(n%k==0)printf(" -1\n"); //正好可以分为n/k块,并且打印最后一块
else {
printf(" %05d\n",nds[(i+1)*k].adr);
for(int j=n/k*k; j<n; j++) {
printf("%05d %d",nds[j].adr,nds[j].data);
if(j<n-1)printf(" %05d\n",nds[j+1].adr);
else printf(" -1\n");
}
}
}
}
return 0;
}
PAT Advanced 1074 Reversing Linked List (25) [链表]的更多相关文章
- PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed ...
- PAT甲级1074 Reversing Linked List (25分)
[程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...
- PAT甲题题解-1074. Reversing Linked List (25)-求反向链表
题意说的很清楚了,这种题的话,做的时候最好就是在纸上自己亲手模拟一下,清楚一下各个指针的情况, 这样写的时候就很清楚各个指针变量保存的是什么值. PS:一次AC哈哈,所以说自己动手在纸上画画还是很有好 ...
- PAT (Advanced Level) 1074. Reversing Linked List (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT 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 elem ...
- 【PAT甲级】1074 Reversing Linked List (25 分)
题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...
- PAT A1133 Splitting A Linked List (25) [链表]
题目 Given a singly linked list, you are supposed to rearrange its elements so that all the negative v ...
- 1074. Reversing Linked List (25)
模拟题,注意当k == 1 与 k == n时情况 #include <stdio.h> #include <string.h> #include <iostream&g ...
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
随机推荐
- ZOJ 3795 Grouping 强连通分量-tarjan
一开始我还天真的一遍DFS求出最长链以为就可以了 不过发现存在有向环,即强连通分量SCC,有向环里的每个点都是可比的,都要分别给个集合才行,最后应该把这些强连通分量缩成一个点,最后保证图里是 有向无环 ...
- Windows + Python + flup + flask + fastcgi + Nginx配置
Nginx配置 # HTTPS server { listen ssl; server_name kvaccount.xx.io; ssl_certificate "C:/xx/conf/s ...
- vim使用技巧(常用指令)
1. vim基础操作 vim是从 vi 发展出来的一个文本编辑器 .代码补完.编译及错误跳转等做了一些增强 1.1 进入编辑模式 命令 含义 i和I i在光标前插入,I在行首插入. a和A a在光标后 ...
- js模式-观察者模式
// 主题,接收状态变化,触发每个观察者 class Subject { constructor() { this.state = 0 this.observers = [] } getState() ...
- Maccms后门分析复现(并非官网的Maccms){10.15 第二十二天}
该复现参考网络中的文章,该漏洞复现仅仅是为了学习交流,严禁非法使用!!!! Maccms官网:http://www.maccms.cn/ Maccms网站基于PHP+MYSQL的系统,易用性.功能良好 ...
- hibernate注解 笔记
1.hibernate使用@where实现条件过滤功能 其里面只有一个参数clause,完整用法是: @Where(clause = "VALID_FLAG=1") 可以加在实体类 ...
- JavaScript面试题(转)
JS相关问题 数组去重 function uniq(array){ var temp = []; //一个新的临时数组 for(var i = 0; i < array.length; i++) ...
- 15. react UI组件和容器组件的拆分 及 无状态组件
1.组件的拆分 组件拆分的前提 当所有的逻辑都出现在一个组件内时 组件会变得非常复杂 不便与代码的维护 所以对组件进行拆分 IU组件 进行页面渲染 容器组件 进行逻辑操作 UI组件的拆分 新建一个 ...
- unique constraint(PD.HSI_RIGHT) violated
插入时报错,原因:唯一约束重复了.... 查看表中有唯一约束的列是不是已经有值了 果然,唯一约束有两条重复的记录. ------------------------------------------ ...
- 201771010123汪慧和《面向对象程序设计Java》第十二周实验总结
一.理论部分 1.在Java提供的GUI构建工具中可以分为组件和容器两类. 2.在Java中的组件有:按钮.标签.复选框.单选按钮.选择框.列表框.文本框.滚动条.画布.菜单. 3.在Java中的容器 ...