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 ...
随机推荐
- 正确遍历删除List中的元素方法(推荐)
遍历删除List中的元素有很多种方法,当运用不当的时候就会产生问题.下面主要看看以下几种遍历删除List中元素的形式: 1.通过增强的for循环删除符合条件的多个元素 2.通过增强的for循环删除符合 ...
- BEA公司的weblogic是什么?有什么特点?
转自:http://zhidao.baidu.com/link?url=J9obKwHhuh1sdLoBC3pILeaq1nz_tcpScggBNeS3D0GzAz9FI002vlS2xxJD4_z6 ...
- Textarea自动适用高度且无滚动条解决方案
今日的系统需要动态显示一项数据库里面的内容,该内容包含换行等格式字符,要求如实的反应在页面上. 最初解决办法是使用textarea控件,代码如下: <textarea style="b ...
- C++11 可变参数模板构造string列表
#include <iostream> #include <cstdint> #include <list> #include <string> tem ...
- $(document).ready()方法和window.onload()方法
$(document).ready()方法和window.onload()方法 $(document).ready()方法是JQuery中的方法,他在DOM完全就需时就可以被调用,不必等待这些元素关联 ...
- onethink 密码加密方式详解
/** * 系统非常规MD5加密方法 * @param string $str 要加密的字符串 * @return string */ function think_ucenter_md5($str, ...
- 蓝的成长记——追逐DBA(6): 做事与做人:小技术,大为人
***********************************************声明*************************************************** ...
- java数据库操作:JDBC的操作
1,JDBC注意操作类及接口: 数据库操作过程: 1)打开数据库服务 2)连接数据库:一般都要输入用户名,密码, 3)操作数据库:创建表:查询表,更新,记录. 4)关闭数据库. 1,DriverMan ...
- :not() 选择器选取除了指定元素以外的所有元素
:not() 选择器选取除了指定元素以外的所有元素; 最近有人那上图问:如果触碰li时背景色和字体都变化,但是已经被选中的(现在是第一行)不变怎么办?:not是一个非常简单方便的办法:加入我给已经被选 ...
- oracle经验小节2
1,instr 函数 在Oracle/PLSQL中,instr函数返回要截取的字符串在源字符串中的位置. 语法如下: instr( string1, string2 [, start_position ...