题目地址

https://pta.patest.cn/pta/test/16/exam/4/question/664

5-2 Reversing Linked List   (25分)

Given a constant KK and a singly linked list LL, you are supposed to reverse the links of every KK elements on LL. For example, given LL being 1→2→3→4→5→6, if K = 3K=3, then you must output 3→2→1→6→5→4; if K = 4K=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 NN (\le 10^5≤10​5​​) which is the total number of nodes, and a positive KK (\le N≤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 NN 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
/*
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-07-08 15:54 答案正确 25 5-2 gcc 131 3
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 12/12 2 1
测试点2 答案正确 3/3 14 1
测试点3 答案正确 2/2 1 1
测试点4 答案正确 2/2 2 1
测试点5 答案正确 2/2 2 1
测试点6 答案正确 3/3 131 3
测试点7 答案正确 1/1 2 1
*/
#include<stdio.h>
#define MAXLEN 100002
struct node {
int data;
int next;
}; int k,head; struct node workArray [MAXLEN]; int Input(struct node array[])
{
int i,inputHead,inputLength;
int index,data,next; scanf("%d %d %d",&inputHead,&inputLength,&k);
for (i=0;i<inputLength;i++){
scanf("%d %d %d",&index,&data,&next);
array[index].data=data;
array[index].next=next;
}
return inputHead;
} int count(int head,struct node array[])
{
int i,cnt=1;
i=head;
while(array[i].next!=-1){
cnt++;
i=array[i].next;
}
return cnt;
}
void PrintList(int head,struct node array[])
{
int idx=head;
while(array[idx].next!= -1){
printf("%05d %d %05d\n",idx,array[idx].data,array[idx].next);
idx=array[idx].next;
}
printf("%05d %d %d",idx,array[idx].data,array[idx].next);
} int ReverseList(struct node array[],int *head,int k)
{
/*
首先用count求链表长度,放到cnt中保存。每次执行cnt自身减掉k,如果cnt<k则不进行翻转
然后使用ptr1 ptr2 ptr3 三个指针
ptr1为当前节点 ptr2为下一个节点 将ptr1->ptr2改为ptr2->ptr1。因为ptr2中的next原有内容会丢失,故用ptr3保存ptr2的下一个节点
执行完一次后,k个节点区间内,头尾互换。
故lastend保存前一区块的末端,是上一区间的头节点
nexthead即下一区块的头结点,同样也是该区块翻转完后的末端。于是提前用lastend=nexthead保存。
*/
int cnt;
if(k==1)
return;
cnt=count(*head,array);
int i,ptr1,ptr2,ptr3,firstflag=0,nexthead=*head,lastend=-2;//ptr1指当前指针,ptr2指下一个要指向ptr1的,ptr3指向还未做反转的下一个。
while(cnt>=k){
// printf("-------head=%d,nexthead=%d,cnt=%d\n",*head,nexthead,cnt);//for_test
ptr1=nexthead;
ptr2=array[ptr1].next;
for(i=1;i<k;i++){
ptr3=array[ptr2].next;
array[ptr2].next=ptr1;
ptr1=ptr2;
ptr2=ptr3; } array[nexthead].next=ptr3;//主要反转做完后,重新定义头尾节点的指向。
if(firstflag==0){
lastend=nexthead;
*head=ptr1;//因为在循环中最后改变了ptr2的值,所以此处用ptr1 。 }
else{
array[lastend].next=ptr1;
lastend=nexthead;
} firstflag++;
nexthead=ptr2;
cnt-=k;
}
} int main()
{ head=Input(workArray);
ReverseList(workArray,&head,k);
PrintList(head,workArray);
}

  

 

PTA 02-线性结构3 Reversing Linked List (25分)的更多相关文章

  1. 02-线性结构3 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  2. pat02-线性结构1. Reversing Linked List (25)

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

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

  4. [刷题] PTA 02-线性结构3 Reversing Linked List

    链表逆序 1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 using namesp ...

  5. 数据结构练习 02-线性结构2. Reversing Linked List (25)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  6. 浙大数据结构课后习题 练习二 7-2 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  7. 【PAT甲级】1074 Reversing Linked List (25 分)

    题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...

  8. PAT甲级1074 Reversing Linked List (25分)

    [程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...

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

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

随机推荐

  1. solr的多条件组合查询和solr的范围查询【转】

    solr的多条件组合查询和solr的范围查询 版权声明:本文为博主原创文章,供大家参考,但不要抄袭哦! 存在问题:为了减轻数据库的访问压力,往往我们将必要的数据存储到solr中,并给部分字段建立索引, ...

  2. List<DTO>转 Map<String,List<DTO>> 两种写法

    List<TeamScheduleDTO> list = JSON.parseArray(response.getData().getJSONArray("list") ...

  3. iOS自带TTS技术的实现即语音播报

    文本转语音技术, 也叫TTS, 是Text To Speech的缩写. iOS如果想做有声书等功能的时候, 会用到这门技术. 一,使用iOS自带TTS需要注意的几点: iOS7之后才有该功能 需要 A ...

  4. spring Existing transaction found for transaction marked with propagation 'never' 解决

    先在申明事务中配置了所有的事务 <!--配置事物传播策略,以及隔离级别--> <tx:advice id="txAdvice" transaction-manag ...

  5. 【HEVC简介】High Level Syntax

    参考文献:见<High Efficiency Video Coding (HEVC)>High Level Syntax章节 <HEVC标准介绍.HEVC帧间预测论文笔记>系列 ...

  6. CSS 循环动画效果。

    @-moz-keyframes revolving{ 0{ -moz-transform: rotate(0deg); -webkit-transform: rotate(0deg); } 25%{ ...

  7. python笔记_magic变量和函数

    前言 先扯一点背景知识 PEP8(Python Enhancement Proposal)是一份python的编码规范,链接:http://www.python.org/dev/peps/pep-00 ...

  8. 云原生技术图谱 (CNCF Landscape)

    转自:https://raw.githubusercontent.com/cncf/landscape/master/landscape/CloudNativeLandscape_latest.jpg

  9. 11gR2新特性---gipc守护进程

    在这篇文章中,我们会对11gR2 新的守护进程gipcd(资源名称ora.gipcd)进行介绍,其中包括gipc的功能,启动顺序和一些基本的测试. 我们知道,对于oracle集群来说,集群私网是非常重 ...

  10. centos7 设置grub密码及单用户登录实例

    centos7与centos6在设置grub密码的操作步骤上有很大的差别,特此记录供以后查用 grub加密的目的: 防止不法分子利用单用户模式修改root密码 给grub加密可以采用明文或者加密的密文 ...