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=; 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=;i<N;++i)
{
Node node;
scanf("%d %d %d",&node.address,&node.data,&node.next);
nodes[node.address]=node;
}
int address=fnAddress;
while(address!=-)//去噪
{
list.push_back(nodes[address]);
address=nodes[address].next;
}
int size=list.size();
int round=size/K;
int start,end;
for(i=;i<=round;++i)
{
start=(i-)*K;
end=i*K;
reverse(list.begin()+start,list.begin()+end);
}
for(i=;i<size-;++i)
{
printf("%.5d %d %.5d\n",list[i].address,list[i].data,list[i+].address);
}
printf("%.5d %d %d\n",list[i].address,list[i].data,-);
return ;
}

PAT 1074. Reversing Linked List (25)的更多相关文章

  1. 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 ...

  2. PAT 1074 Reversing Linked List[链表][一般]

    1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...

  3. 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 e ...

  4. PAT (Advanced Level) 1074. Reversing Linked List (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  5. PAT甲题题解-1074. Reversing Linked List (25)-求反向链表

    题意说的很清楚了,这种题的话,做的时候最好就是在纸上自己亲手模拟一下,清楚一下各个指针的情况, 这样写的时候就很清楚各个指针变量保存的是什么值. PS:一次AC哈哈,所以说自己动手在纸上画画还是很有好 ...

  6. 【PAT甲级】1074 Reversing Linked List (25 分)

    题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...

  7. PAT甲级1074 Reversing Linked List (25分)

    [程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...

  8. 1074. Reversing Linked List (25)

    模拟题,注意当k == 1 与 k == n时情况 #include <stdio.h> #include <string.h> #include <iostream&g ...

  9. PAT 1074. Reversing Linked List

    #include <cstdio> #include <cstdlib> #include <iostream> #include <unordered_ma ...

随机推荐

  1. XML1_XML基础

    1.XML的作用 XML 被设计用来传输和存储数据.所以XML 是不作为的. 2.简单的描述 XML 文档形成一种树结构. XML 文档必须包含根元素.该元素是所有其他元素的父元素.XML 文档中的元 ...

  2. 互联网HTTP连接等出错代码大全

    100 - Continue  101 - Switching Protocols Success Codes  200 - OK  201 - Created  202 - Accepted  20 ...

  3. 版本控制工具git入门

    版本控制工具的历史 不说了,放张图 两者的区别:集中式需要一个中心服务器放置最新的文件,需要联网操作.分布式可以再不联网的情况下操作,前提要拥有版本库 git安装  略 github注册 略 如何在g ...

  4. 定位 - MapKit - 基本使用

    /** *  Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Co ...

  5. Ubuntu 安装 JDK 7 / JDK8 的两种方式

    ubuntu 安装jdk 的两种方式: 1:通过ppa(源) 方式安装. 2:通过官网下载安装包安装. 这里推荐第1种,因为可以通过 apt-get upgrade 方式方便获得jdk的升级 使用pp ...

  6. BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花

    Description Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the g ...

  7. ANDROID_MARS学习笔记_S04_008_用Listview、自定义adapter显示返回的微博数据

    一.简介 运行结果 二.代码1.xml(1)activity_main.xml <?xml version="1.0" encoding="utf-8"? ...

  8. leetcode面试准备: Substring with Concatenation of All Words

    leetcode面试准备: Substring with Concatenation of All Words 1 题目 You are given a string, s, and a list o ...

  9. insert into select 堵塞update

    mysql[192.168.5.15] blocking_thread[2286333] blocking_query[insert into temp_zhuyou_mktact_1(hotel_g ...

  10. Learning WCF Chapter1 Creating a New Service from Scratch

    You’re about to be introduced to the WCF service. This lab isn’t your typical “Hello World”—it’s “He ...