PAT002 Reversing Linked List
题目:
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 分析:主要考查链表逆序。 如果通过数组进行排序,然后每K个逆序输出,对于多余的节点将无法通过测试,所以实现必须使用链表。
代码:
typedef struct reverseNode { long address;
int value;
long nextAdd;
struct reverseNode *next; } ReverseNode; ReverseNode *reverseLink(ReverseNode *head, int reverseLen)
{
ReverseNode *new = head->next;
ReverseNode *old = new->next;
int count = ;
while (count < reverseLen) {
ReverseNode *temp = old->next;
old->next = new;
old->nextAdd = new->address;
new = old;
old = temp;
count++;
}
head->next->next = old;
if (!old) {
head->next->nextAdd = -;
} else {
head->next->nextAdd = old->address;
}
ReverseNode *temp = head->next;
head->next = new;
head->nextAdd = new->address;
return temp;
} int main()
{
// 读取输入
long beginAddress;
int number, reverseLen;
scanf("%ld %d %d", &beginAddress, &number, &reverseLen); ReverseNode *head = (ReverseNode *)malloc(sizeof(ReverseNode));
ReverseNode *a[number]; for (int i = ; i < number; i++) {
long address, nextAdd;
int value;
scanf("%ld %d %ld",&address, &value, &nextAdd);
ReverseNode *node = (ReverseNode *)malloc(sizeof(ReverseNode));
node->address = address;
node->value = value;
node->nextAdd = nextAdd;
node->next = ;
a[i] = node;
if (beginAddress == address) {
head->next = node;
}
} // 对输入数据通过链表连接起来
ReverseNode *temp = head->next;
int actualNumber = ;
int recyCount = number;
while (temp->nextAdd != - && recyCount-- > ) {
for (int i = ; i < number; i++) {
ReverseNode *tempNode = a[i];
if (tempNode->address == temp->nextAdd) {
temp->next = tempNode;
temp->nextAdd = tempNode->address;
temp = temp->next;
actualNumber++;
break;
}
}
} // 反转
if (reverseLen > ) {
int reverseCount = actualNumber / reverseLen; // 需要进行反转的次数
ReverseNode *tempHead = head;
while (reverseCount-- > ) {
tempHead = reverseLink(tempHead, reverseLen);
}
} ReverseNode *ptr = head->next;
while (ptr) {
if (ptr->nextAdd == -) {
printf("%.5ld %d -1\n", ptr->address, ptr->value);
} else {
printf("%.5ld %d %.5ld\n", ptr->address, ptr->value, ptr->nextAdd);
}
ptr = ptr->next;
}
}
运行结果:
[测试点5,是使用超级大量数据,由于我在将输入数据通过链表连接起来那一步采用的是嵌套循环,复杂度O(N^2),可能是这里导致的,不确定。 但是总体反转思路是这样的,后面找到原因了再进行更新]
PAT002 Reversing Linked List的更多相关文章
- PAT1074 Reversing Linked List (25)详细题解
02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
- pat02-线性结构1. Reversing Linked List (25)
02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...
- 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 ...
- 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 ...
- 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 ...
- PAT_A1074#Reversing Linked List
Source: PAT A1074 Reversing Linked List (25 分) Description: Given a constant K and a singly linked l ...
- 02-线性结构3 Reversing Linked List
02-线性结构3 Reversing Linked List (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...
- 想了很久,一道Microsoft的笔试题目 —— Reversing Linked List
Reversing Linked List Given a constant K and a singly linked list L, you are supposed to reverse the ...
随机推荐
- 更改Mysql数据库存储位置
默认安装位置 C:\Program Files\MySQL\MySQL Server 5.7 一.首先把mysql的服务先停掉. 二.更改MySQL配置文件My.ini中的数据库存储主路径 打开MyS ...
- JAVA基础(10)——IO、NIO
转载:http://blog.csdn.net/weitry/article/details/52964948 JAVA基础系列规划: JAVA基础(1)——基本概念 JAVA基础(2)——数据类型 ...
- Node.js制作图片下载爬虫的一般步骤
图片下载爬虫分两部分:爬页面和下载图片. 爬页面时先看网址是https还是http的,然后选择不同的内置对象: 其次看编码,如果是charset=gb2312的网页就需要iconv帮忙转码,好在大部分 ...
- linux内核及其模块的查询,加载,卸载 lsusb等
http://blog.sina.com.cn/s/blog_53e81e2a0100zkxi.html 1,/sbin/update-modules文件,他是一个linux通用的模块管理脚本程序. ...
- static、final修饰符、内部类
static修饰符: static修饰符能够与属性.方法和内部类一起使用,表示静态的.类中的静态变量和静态方法能够与类名一起使用.不须要创建一个类的对象来訪问该类的静态成员. class Static ...
- 排序(2)---------简单插入排序(C语言实现)
插入排序(Insertion Sort)的算法描写叙述是一种简单直观的排序算法. 它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到对应位置并插入.插入排序在实现上,通常 ...
- Com与.Net互操作
Com与.Net互操作 .Net调用Com组件主要分为两类:静态调用及动态调用.所谓静态调用:指通过tlbimp.exe命名产生Com组件在.Net环境下的包装类,然后通过这个包装类来访问Com组件. ...
- Repository与UnitOfWork引入
Repository是什么? 马丁大叔的书上同样也有解释:它是衔接数据映射层和域之间的一个纽带,作用相当于一个在内存中的域对象映射集合,它分离了领域对象和数据库访问代码的细 节.Repository受 ...
- sqlite 小刀 初试
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经在很多嵌入式产 ...
- oc 阿拉伯数字转中文数字
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; formatter.numberStyle = NSNumberFor ...