PAT-A 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 Lbeing 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
思路:给出的节点可能不在链表中,需要特别注意。这个解法复杂了,和一年前的思路完全不同。更简练的代码参考B1025的解法。https://mp.csdn.net/postedit/81151398
#include <queue>
#include <map>
#include <stack>using namespace std;typedef struct node
{int addr;int data;int next;node(){}node(int a, int d, int n):addr(a), data(d), next(n){}
}node;int main()
{int head, n, k;scanf("%d%d%d", &head, &n, &k);map<int, node> all;for(int i = 0; i < n; i++){int addr, data, next;scanf("%d%d%d", &addr, &data, &next);all[addr] = node(addr, data, next);}queue<node> link;int len = 0;while(head != -1){link.push(all[head]);head = all[head].next;len++;}stack<node> work;queue<node> reversed;int group = len / k; //需要逆置的组数for(int i = 0; i < group; i++){for(int j = 0; j < k; j++){work.push(link.front());link.pop();}if(!reversed.empty())reversed.back().next = work.top().addr;while(!work.empty()){reversed.push(work.top());work.pop();if(!work.empty())reversed.back().next = work.top().addr;}}while(!link.empty()){reversed.back().next = link.front().addr;reversed.push(link.front());link.pop();}reversed.back().next = -1;while(!reversed.empty()){node tem = reversed.front();int addr = tem.addr;int data = tem.data;int next = tem.next;printf("%05d %d ", addr, data);if(next == -1)printf("-1\n");elseprintf("%05d\n", next);reversed.pop();}return 0;
}
PAT-A 1074 Reversing Linked List (25 分)相关推荐
- 1074 Reversing Linked List (25 分) java 题解
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- 1074 Reversing Linked List (25 分)【难度: 一般 / 知识点: 链表】
https://pintia.cn/problem-sets/994805342720868352/problems/994805394512134144 乙级里面的原题吧,就用哈希表建链表,reve ...
- PAT甲级1074 Reversing Linked List :[C++题解]反转链表,借用vector
文章目录 题目分析 题目链接 题目分析 分析:数组模拟链表,这题反转操作在数组中进行,然后直接输出即可,甚至不用放回到链表. //遍历链表,该链表用数组模拟 //保存链表结点地址到数组中 for(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 ...
- 1074. Reversing Linked List (25)-PAT甲级真题
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- 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 ...
- 02-线性结构3 Reversing Linked List (25 分)
如果 单纯从做题角度来讲,上一个做法当然没问题,可是目的是为了练习静态链表,so还是不要投机取巧了,静态链表解法如下: #include <stdio.h> struct data {in ...
- (c语言)Reversing Linked List (25分)
关于数据结构Mooc后的每一道答案 基本我都已经给出了详解 希望能对大家有所帮助 收藏一下也是方便大家查找吧 希望大家一起进步! (c语言)浙大数据结构Mooc作者答案集 下面是关于这道题的图片 关于 ...
- 附加3 Merging Linked Lists (25 分)
Given two singly linked lists L 1=a 1 →a 2 →⋯→a n−1 →a n and L 2 =b 1 →b 2 →⋯→b m−1 →b m . If n≥2m ...
- PAT 1074 Reversing Linked List
1074 Reversing Linked List (25point(s)) Given a constant K and a singly linked list L, you are suppo ...
最新文章
- 2020阿里全球数学大赛:3万名高手、4道题、2天2夜未交卷,73人天团,正式出道!
- win10下Anaconda如何查看PyTorch版本
- Oracle 优化器_表连接
- NG-ZORRO 表格多选框改为单选框 (angular框架)
- python某公司为员工发放奖品_python 练习2
- 解决nginx无法启动的问题——端口被占用
- docker安装hbase
- php pathinfo()函数
- 摆脱臃肿--Unity3D安卓包减肥秘笈
- day3-python之函数初识(二)
- 监控指标分类汇总归纳
- mysql 5.1.71_MySQL升级从5.1.71到5.7.17
- joc杂志影响因子2019_化学sci期刊影响因子排名_国际化学期刊2018最新影响因子_分析测试学报影响因子...
- Windows - 电脑屏幕眼睛保护色的取值
- macOS linux 并发测试工具 wrk
- 在阿里云开源镜像站中下载centOS7
- 分享一些常用软件序列号及注册码
- 降低网站跳出率9种方法
- 明源售楼系统技术解析 房源生成(二)
- 哎我就不信了,Java IO有这么难吗?
热门文章
- 某企业采购腐败处理办法
- leetcode202快乐数(JAVA版)
- Wise Installation制作的安装包添加卸载快捷方式
- php lcg value与mt rand生成0 1随机小数的效果比较
- javascript的一点点学习记录
- Java-Problems
- Android应用开发 00:Jetpack Compose学习 生日贺卡 图片 Compose象限 名片
- (2008-10-31)山西五日自助游记
- 无线Mesh网络技术
- 华为双前置摄像头_国产手机集体爱上“双打孔屏”,前置双摄像头将成2020年标配?...