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 提交了10次才完全正确,花了快5个小时。感觉自己实力好差。
#include<iostream>
#include <string>
using namespace std;
class linked{
public:
int data;
int next;
};
linked *list = new linked[];
int prehead;
int nexthead;
int Reverse(int head, int k){
int cnt = ,temp;
int news = head;
int olds = list[head].next;
while (cnt < k){
temp = list[olds].next;
list[olds].next = news;
news = olds;
olds = temp;
cnt++;
}
list[head].next = olds;
prehead = head;
nexthead = olds;
return news;
}
int main(){
int firstNode, address, next,N, k, data,head,preshead;
int sum = ;
cin >> firstNode >> N >> k;
for (int i = ; i < N; i++){
cin >> address >> data >> next;
list[address].data = data;
list[address].next = next;
}
int p = firstNode;
while (p != -){
sum++;
p = list[p].next;
}
head = Reverse(firstNode, k);
for (int i = ; i < sum / k; i++){
preshead = prehead;
list[preshead].next = Reverse(nexthead, k);
}
p = head;
while (p!= -){
printf("%05d ",p);
cout << list[p].data <<" ";
if (list[p].next != -)
printf("%05d\n", list[p].next);
else
cout << list[p].next << endl;
p = list[p].next;
} return ;
}

ac的图片看着真的很爽。

数据结构练习 02-线性结构2. Reversing Linked List (25)的更多相关文章

  1. pat02-线性结构1. Reversing Linked List (25)

    02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...

  2. PTA 02-线性结构3 Reversing Linked List (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List   (25分) Given a ...

  3. 02-线性结构3 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 ...

  4. 浙大数据结构课后习题 练习二 7-2 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 ...

  5. 02-线性结构3 Reversing Linked List

    02-线性结构3 Reversing Linked List   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...

  6. 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】

    02-线性结构3 Reversing Linked List(25 point(s)) Given a constant K and a singly linked list L, you are s ...

  7. Java数据结构介绍(线性结构和非线性结构)

    数据结构包括:线性结构和非线性结构. 线性结构 数据元素之间存在一对一的线性关系 包括顺序存储结构和链式存储结构.顺序存储的线性表称为顺序表,顺序表中的存储元素是连续的 链式存储的线性表称为链表,链表 ...

  8. PAT1074 Reversing Linked List (25)详细题解

    02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

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

随机推荐

  1. C# - 集合类 - 集合接口

    本篇将介绍关于集合的接口 这些接口定义了所有与集合有关的类的框架 IEnumerable接口 ns:System.Collections 此接口定义了对集合遍历的方法 一般表示元素序列或集合的类都实现 ...

  2. Default route and zero route

    A default route of a computer that is participating in computer networking is the packet forwarding ...

  3. hdu2565java

    放大的X Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...

  4. 【腾讯Bugly干货分享】RecyclerView 必知必会

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:http://mp.weixin.qq.com/s/CzrKotyupXbYY6EY2HP_dA 导语 Re ...

  5. Sun开发的JINI技术在网络中的应用

    一.概述 Sun公司开发的Jini系统是一种创新技术,听说它的人很多但较少被人理解.Jini系统可利用Java扩展网络计算的范围,并有可能成为最佳解决方案. Jini是Sun公司的研究与开发项目,它能 ...

  6. Maximum Product Subarray动态规划思想

    该题即是昨天没有做出来的题目,想了很久,想出了一个普通的做法,提交发现超时了.思想是新建一个数组,保存每个元素与后面的元素相乘后得到的最大值,然后再在该数组中选出最大的值,返回.[笨死 发现行不通后决 ...

  7. C#基础整理

    元旦整理书架发现一本小册子——<C#精髓>中国出版社2001年出版的,粗略翻了下关于C#的知识点挺全的虽然内容谈得很浅也有很多过时的内容(话说这本书是我在旧书店花5块钱淘的)我保留原有章节 ...

  8. jQuery 常用代码集锦

    1. 选择或者不选页面上全部复选框 var tog = false; // or true if they are checked on load $('a').click(function() { ...

  9. 04_XML_01_入门基础

    [什么是XML] Extensible Markup Language,翻译过来即可扩展标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 在XML语言中,它允 ...

  10. 01_根据Id查询User的数据

    [工程目录] [数据库表中内容 user表] [sqlMapConfig.xml配置文件主要内容] 简述:sqlMapConfig.xml配置文件主要有两个作用: 1.配置和数据连接的相关信息,例如事 ...