题目:

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的更多相关文章

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

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

  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. pat02-线性结构1. Reversing Linked List (25)

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

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

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

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

  7. PAT_A1074#Reversing Linked List

    Source: PAT A1074 Reversing Linked List (25 分) Description: Given a constant K and a singly linked l ...

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

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

  9. 想了很久,一道Microsoft的笔试题目 —— Reversing Linked List

    Reversing Linked List Given a constant K and a singly linked list L, you are supposed to reverse the ...

随机推荐

  1. TestNG 七 annotation

    TestNG中用到的annotation的快速预览及其属性. @BeforeSuite:   被注释的方法将在所有测试运行前运行 @AfterSuite:  被注释的方法将在所有测试运行后运行 @Be ...

  2. FastIV图像处理

    新建一图像处理算法群,主要讨论图像处理与计算机视觉中的快速算法及其工程实现. 群号码:322687422

  3. 集合系列之fail-fast 与fail-safe 区别

    一:快速失败(fail—fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了修改(增加.删除.修改),则会抛出Concurrent Modification Exceptio ...

  4. 我的PHPMailer_v5.1 使用

    <?php /** * Simple example script using PHPMailer with exceptions enabled * @package phpmailer * ...

  5. websphere中的会话超时设置 和 web应用中web.xml中session-timeout关系

    Tomcat默认的会话的超时时间设置 设置Tomcat session有效期的三种方式有: 1.在tomcat/conf/web.xml中修改session-timeout的值,该设置是TOMCAT全 ...

  6. mui webview操作

    HBuilder的webview操作 webviewAPI文档:http://www.html5plus.org/doc/zh_cn/webview.html 创建新的webview窗口: Webvi ...

  7. Navicat Premium如何打开SQL文件.MDF和.LDF文件

    相信大家再装SQL Server时都会遇到一件很头疼的事情--装完SQL server之后发现没有启动程序.没有经验的会以为SQL SERVER安装失败了于是选择重装.可是呵呵,重装了4,5次还是一样 ...

  8. Android 6.0 超级简单的权限申请 (Permission)

    代码地址如下:http://www.demodashi.com/demo/13369.html 背景描述 随着Android系统的不断升级,谷歌对用户的隐私是越来越注重了,给我们开发者带来了更多的繁琐 ...

  9. Python-常用字符串转换实例

    当字符串是:'\u4e2d\u56fd' >>>s=['\u4e2d\u56fd','\u6e05\u534e\u5927\u5b66']>>>str=s[0].d ...

  10. PHP 导出 CSV 文件用 Excel 打开出现中文乱码

    本篇文章由:http://xinpure.com/php-export-csv-file-opened-by-excel-appear-garbled/ 乱码情况 写了一段导出 CSV 文件的代码,可 ...