PAT 1074 Reversing Linked List
1074 Reversing Linked List (25point(s))
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
思路
数据结构可以选用“静态链表”,其中,静态链表的结点可以设置一个变量用于表示结点性质
结点性质一般用于表示结点是否在链表中、结点是否有效、或者反映结点在链表中的位置等等
结点性质可用于排序,把离散的静态链表结点变成连续存放,方便进行题目要求的操作。
就好像这一题,我们那可以用变量order来记录静态结点的序号,order初始化为MAXN,即数组大小,然后遍历链表结点的时候,把结点的order变量设置成0,1,2,3... maxn-1
这样,我们就可以“对order从小到大排序”,排序后,有效结点(即order<MAXN)都在前面,而且是按照链表的遍历顺序存放
这样的话,对于题目:每k个数翻一次,就容易处理多了,直接利用reverse()函数即可,需要注意的是,最后一个分段的数的个数可能不够k个,这时不需要翻转
当翻转后,我们把每个结点的后继地址修改一下就行
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<unordered_map>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define maxn 100010struct Node {int addr;//记录结点地址,方便后面修改后继地址int data;int order;//结点性质int next;Node() {order = maxn;}
}node[maxn];bool cmp(Node x, Node y) {return x.order < y.order;
}int main() {int n, k, begin, addr, cnt;cin >> begin >> n >> k;for (int i = 0; i < n; i++) {scanf("%d", &addr);node[addr].addr = addr;scanf("%d %d", &node[addr].data, &node[addr].next);}//遍历一次链表,标记orderint p = begin, order = 0;while (p!=-1) {node[p].order = order;p = node[p].next;order++;}n = order;sort(node, node + maxn, cmp);//把离散的静态链表连续化存储//每k个数翻转一次for (int i = 0; i < n; i += k) {if (i + k <= n) {reverse(node + i, node + i + k);}}//修改每个结点的后继地址int i;for (i = 0; i < n - 1; i++) {node[i].next = node[i + 1].addr;}node[n - 1].next = -1;//输出for (i = 0; i < n - 1; i++) {printf("%05d %d %05d\n", node[i].addr, node[i].data, node[i].next);}printf("%05d %d -1\n", node[n - 1].addr, node[n - 1].data);return 0;
}
PAT 1074 Reversing Linked List相关推荐
- 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——双端队列解法
题目地址 原始想法 既然给定的序列是乱序的: 但是输出要求顺序,再按块反转输出 那么我为何不直接在读入的时候调整为顺序呢! 具体思路 读入1个结点后,剩下的结点,仅存在以下3种情况 插在他前面:插在他 ...
- PAT甲级1074 Reversing Linked List :[C++题解]反转链表,借用vector
文章目录 题目分析 题目链接 题目分析 分析:数组模拟链表,这题反转操作在数组中进行,然后直接输出即可,甚至不用放回到链表. //遍历链表,该链表用数组模拟 //保存链表结点地址到数组中 for(in ...
- 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 分) 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)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- 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 elem ...
- PAT 7-2 Reversing Linked List
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 ...
最新文章
- 短信转化效果不好?试试这几招
- 【Xamarin挖墙脚系列:Xamarin4.0的重大变更】
- FPGA学习之路—接口(3)—SPI详解及Verilog源码分析
- 真是个狠人!开学第一天,这批小学生的造型刷爆朋友圈!
- 免费下载谷歌maps软件_Android Google Maps示例教程
- Linux 下Nginx+Tomcat 完美整合 nginx tomcat 整合方式
- Flutter实战之Sqlite的用法入门
- Unity GC垃圾回收
- 11计算机专业vb试题答案,11高三计算机专业VB试题(三)
- 【折腾系列—All In One主机】3、 iKuai软路由安装前的设置
- vue weex 打电话
- matlab八分之一中点画圆算法,中点八分画圆算法
- 神通数据库安装及使用手册
- python ocr文字识别竖排繁体_繁体文字识别 ABBYY 与 千百OCR 使用比较
- MySQL参数优化:back_log
- 1.1.8 怎样在Word的页眉中插入一级标题
- 人工智能之无人驾驶技术到底是怎么回事
- 关于IT信息化的一些感想
- 【论文复现】使用PaddleDetection复现OrientedRepPoints的复现笔记
- 素数,质数,奇数,合数,偶数这些你都能分清吗?
热门文章
- 告别排队!用Python定时自动挂号和快捷查询化验报告
- 2018机器阅读理解技术竞赛,奇点机智获第一名
- 卧槽!成就了Java,开发框架排第一,你还是不够了解它!
- 为什么我不建议你用阿里巴巴Java规范,而使用 Google Guava 编程?
- fork/join 全面剖析,你可以不用,但是不能不懂!
- 如何在Java代码中去掉烦人的“!=null”
- 你还在从零搭建项目 ?
- 什么是好的图神经网络?
- 计算机科学和Python编程导论(三) 一些简单的数值程序
- 两大顶级 AI 算法一起开源!Nature、Science 齐发 Alphafold2 相关重磅,双厨狂喜~...