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 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 简单的模拟题,以空间换时间:
#include <cstdio> #include <vector> #include <algorithm> using namespace std;const int NUM=100001;struct Node {int address;int data;int next; };Node nodes[NUM]; vector<Node> list; int main() {int fnAddress,N,K;scanf("%d %d %d",&fnAddress,&N,&K);int i;for(i=0;i<N;++i){Node node;scanf("%d %d %d",&node.address,&node.data,&node.next);nodes[node.address]=node;}int address=fnAddress;while(address!=-1)//去噪 {list.push_back(nodes[address]);address=nodes[address].next;}int size=list.size();int round=size/K;int start,end;for(i=1;i<=round;++i){start=(i-1)*K;end=i*K;reverse(list.begin()+start,list.begin()+end);}for(i=0;i<size-1;++i){printf("%.5d %d %.5d\n",list[i].address,list[i].data,list[i+1].address);}printf("%.5d %d %d\n",list[i].address,list[i].data,-1);return 0; }
View Code
转载于:https://www.cnblogs.com/wwblog/p/3708838.html
PAT 1074. Reversing Linked List (25)相关推荐
- PAT 1074 Reversing Linked List
1074 Reversing Linked List (25point(s)) Given a constant K and a singly linked list L, you are suppo ...
- 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 ...
- 1074 Reversing Linked List (25 分)【难度: 一般 / 知识点: 链表】
https://pintia.cn/problem-sets/994805342720868352/problems/994805394512134144 乙级里面的原题吧,就用哈希表建链表,reve ...
- PAT 1074 Reversing Linked List——双端队列解法
题目地址 原始想法 既然给定的序列是乱序的: 但是输出要求顺序,再按块反转输出 那么我为何不直接在读入的时候调整为顺序呢! 具体思路 读入1个结点后,剩下的结点,仅存在以下3种情况 插在他前面:插在他 ...
- PAT甲级1074 Reversing Linked List :[C++题解]反转链表,借用vector
文章目录 题目分析 题目链接 题目分析 分析:数组模拟链表,这题反转操作在数组中进行,然后直接输出即可,甚至不用放回到链表. //遍历链表,该链表用数组模拟 //保存链表结点地址到数组中 for(in ...
- 每隔k次反转一次 链表_PTA 5-2 Reversing Linked List (25) [法一] - 线性表 - 链表反转 (PAT 1074)...
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 ...
最新文章
- Enhanced-RCNN: 一种高效的比较句子相似性的方法 |​WWW 2020
- TCP/IP——基本知识
- 我的Ubuntu9.10安装与配置
- vue2移动端使用vee-validate进行表单验证
- (android实战)Service 生命周期和使用注意项
- WCF服务端的.NET Core支持项目Core WCF 正式启动
- 将一个实体转换成 Url 参数的形式 ?a=ab=b
- disBalancer(DDOS)将于4月15日在DuckSTARTER进行IDO
- 8Linux磁盘划分、RAID
- ashx在web.config中如何配置_网络中,什么是半双工与全双工?它们如何配置
- oracle xsql 详解(一)
- ApacheCon Asia 2021 重磅开启!流处理专题 Call for Speaker
- 【收藏】十个深度学习和机器学习问题速问速答
- 使用Canvas合成多张图片和文字为一张图片
- 【淘宝0元购】,所有人无门槛参与!
- wordpress插件_哪个是最好的WordPress画廊插件? (性能比较)
- 台风怎么看内存颗粒_光威TYPE普通马甲内存条,南亚ADIE HR颗粒稳超3200MHZ
- PHP学习线路图:四个阶段
- 知识图谱学习(二):电商知识图谱
- AD9361收发通道原理和配置(AD射频随笔01)
热门文章
- python函数结构_PYTHON 之结构体,全局变量,函数参数,lambda编程 等
- 万维网发布服务 w3svc 已停止 除非万维_万维网和互联网的区别
- 打造最强加密工具之《绝密信息传递》
- html 页面重复度高,哪些情况容易造成重复页面
- jedis watch Java_jedis操作redis的几种常见方式总结
- map for循环_JavaScript 用 for 循环太 low?你是不是有什么误解
- ModuleNotFoundError: No module named ‘apt_pkg‘
- 使用eclipse调试ns3配置说明
- VGA光纤收发器特点及参数
- 工业级交换机的5个常见的优点解析