题目:

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. 开机自启动:从busybox到debian

    需要在mint上设置opensips的开机自启动,翻了半天资料还是一知半解.最后在opensips的官方文档,查到用下面的语句,添加自启动成功.不过貌似还是会有启动不成功,没有仔细测试过. updat ...

  2. css position: relative | absolute | static | fixed详解

    static(静态):没有特别的设定,遵循基本的定位规定,不能通过z-index进行层次分级. fixed(固定定位):这里所固定的参照对象是可视窗口而并非是body或是父级元素.可通过z-index ...

  3. python和c#通用一致的des加密采用CBC和PKCS7

    在python下可以下载pydes 下载地址为 http://pydes.sourceforge.net/ 在c#下实现des加密较为简单,如下: using System; using System ...

  4. hadoop权威指南(第四版)要点翻译(4)——Chapter 3. The HDFS(1-4)

    Filesystems that manage the storage across a network of machines are called distributed filesystems. ...

  5. android项目解刨之时间轴

    近期开发的app中要用到时间轴这东西.须要实现的效果例如以下: 想想这个东西应该能够用listview实现吧. 然后近期就模拟着去写了: 首先写  listview的item的布局: listview ...

  6. struts2 页面向Action传参方式

    1.基本属性注入 我们可以直接将表单数据项传递给Action,而Action只需要提供基本的属性来接收参数即可,这种传参方式称为基本属性注入.例如 jsp页面: <s:form method=& ...

  7. 【LeetCode】- Search Insert Position(查找插入的位置)

    [ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...

  8. CSS3使用Animation为同一个元素添加多个动画效果

    本篇文章由:http://xinpure.com/css3-animation-for-the-same-element-multiple-animation-effects/ CSS3 Animat ...

  9. [Java基础]List,Map集合总结

    java.util包下: Collection    |--List 接口 |----ArrayList |----LinkedList |----Vector |-----Stack |---Set ...

  10. Js日常笔记之this

    在javascript中自己创建构造函数时可以利用this来指向新创建的对象上.这样就可以避免函数中的this指向全局了,如下 var x = 2; function test(){ this.x = ...