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 ...
随机推荐
- You Gotta Care About the Code
You Gotta Care About the Code Pete Goodliffe IT DOESN'T TAKE SHERLOCK HOLMES to work out that good p ...
- [Functional Programming ADT] Debug a Functional JavaScript composeK Flow
When using ADTs in our code base, it can be difficult to use common debugging tools like watches and ...
- .net framework中重新注册IIS
要为 ASP.NET 修复 IIS 映射,请按照下列步骤执行操作:运行 Aspnet_regiis.exe 实用工具:单击“开始”,然后单击“运行”.在“打开”文本框中,键入 cmd,然后按 ENTE ...
- TestNG系列之三:TestNG忽略测试
有时,我们的代码是没有准备好,如果测试用例写入到测试方法/代码将无法运行,在这种情况下, @Test(enabled = false)有助于禁用此测试案例.
- 使用jsmin压缩javascript脚本
官方地址:http://www.crockford.com/javascript/jsmin.html 点击页下方的”zip file containing an MS-DOS.exe file“下载 ...
- xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cycle” in ARC-enabled code
xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cyc ...
- LoadRunner字符串处理 - 补齐字符串
有些时候需要在某个字符串的前面用0补齐,以便满足长度的格式要求. 在LoadRunner中可以封装出一个函数来处理这种问题: /* Function to pad a string to x char ...
- mongodb的基本语法(二)
一.聚集集合查询 1.查询所有记录 db.userInfo.find(); 相当于:select* from userInfo; 默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一 ...
- ASP.NET MVC3 系列教程 - 模型
I:基础绑定的实现 1.在前面的两篇基础文章(路由 及 控制器&视图)当中,还没对QueryString的绑定进行介绍,因为我觉得它更适合放在这一章节中去介绍.我们在用WebForm去开发的时 ...
- 资源管理器也玩多标签:QT TabBar v1.5.0.0a3
http://zmingcx.com/explorer-also-play-more-tags-qt-tabbar-v1-5-0-0a3.html浏览器中的标签浏览功能非常受欢迎,安装QT TabBa ...