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

分析:把地址为temp的数的数值存入data[temp]中,把temp的下一个结点的地址存入next[temp]中。

注意:不一定所有的输入的结点都是有用的,加个计数器sum

#include <iostream>
using namespace std;
int main() {int first, k, n, sum = 0;cin >> first >> n >> k;int temp, data[100005], next[100005], list[100005], result[100005];for (int i = 0; i < n; i++) {cin >> temp;cin >> data[temp] >> next[temp];}while (first != -1) {list[sum++] = first;first = next[first];}for (int i = 0; i < sum; i++) result[i] = list[i];for (int i = 0; i < (sum - sum % k); i++)result[i] = list[i / k * k + k - 1 - i % k];for (int i = 0; i < sum - 1; i++)printf("%05d %d %05d\n", result[i], data[result[i]], result[i + 1]);printf("%05d %d -1", result[sum - 1], data[result[sum - 1]]);return 0;
}

1074. Reversing Linked List (25)-PAT甲级真题相关推荐

  1. 1020. Tree Traversals (25) PAT甲级真题

    之前我看了这道题,实在是看不懂网上的解题答案,他们的具体思路基本上就是通过后续遍历和中序遍历,直接推出层次遍历. 我苦思冥想了半天,是在没看懂这种思路,于是想了一个笨点的但是也比较好理解的思路,通过后 ...

  2. 1078. Hashing (25)-PAT甲级真题

    1078. Hashing (25) The task of this problem is simple: insert a sequence of distinct positive intege ...

  3. 1032. Sharing (25)-PAT甲级真题

    To store English words, one method is to use linked lists and store a word letter by letter. To save ...

  4. 1040. Longest Symmetric String (25)-PAT甲级真题

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...

  5. 1006. Sign In and Sign Out (25)-PAT甲级真题

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  6. 1121. Damn Single (25)-PAT甲级真题

    "Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are suppo ...

  7. 1090. Highest Price in Supply Chain (25)-PAT甲级真题

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  8. 1106. Lowest Price in Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  9. 1114. Family Property (25)-PAT甲级真题(并查集)

    This time, you are supposed to help us collect the data for family-owned property. Given each person ...

最新文章

  1. Local模式下Spark程序只输出关键信息
  2. oozie的作业调度
  3. python工作招聘-爬了招聘网站之后,给你几点学习Python的建议
  4. 请问以前在这儿下载的东西
  5. reactjs redux chrome扩展插件
  6. pythonimportre_Python Re注意问题(持续更新)
  7. asyncio协程与并发
  8. HTML5新增的video标签,HTML5中video标签的使用方法
  9. 用c语言编译二叉树,C语言 数据结构平衡二叉树实例详解
  10. C++语言基础 —— 数组
  11. 安卓应用安全指南 5.3.3 将内部账户添加到账户管理器 高级话题
  12. linux系统中如何安装qwt,在Linux上部署Qt及Qwt
  13. html绘制圆形和弧形的代码,html5 canvas用来绘制弧形的代码实现
  14. iOS开发之--NSPredicate
  15. mysql单表查询怎么做_mysql单表查询
  16. Mybatis原理解析(二)SqlSession的创建过程
  17. android mp4视频下载测试地址亲测有效
  18. 添加内核驱动模块(3)(mydriver.c+ Konfig+Makefile )
  19. 微软高性能缓存AppFabric(二)使用
  20. 22考研英语高频词汇

热门文章

  1. 在 UML 静态结构图中使用类和对象
  2. Spring注入方式及用到的注解 -----@Component,@Service,@Controller,@Repository
  3. left join条件放在on和where的区别
  4. 企业微信jssdk分享接口管理系统
  5. CSS网页布局垂直居中整理
  6. ubuntu下不同版本python默认切换
  7. 打造安全可靠的研发中心桌面
  8. ESXi 5.1 安装 Mac OSX Lion 10.7
  9. 美国政府召开网络安全峰会,与私营行业巨头合力提振软件供应链和开源等安全...
  10. VMware 修复 NSA 报告的 0day