题目地址

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. 基于python的request库,模拟登录csdn博客

    以前爬虫用urllib2来实现,也用过scrapy的爬虫框架,这次试试requests,刚开始用,用起来确实比urllib2好,封装的更好一些,使用起来简单方便很多. 安装requests库     ...

  2. React.js 的 context

    这一节我们来介绍一个你可能永远用不上的 React.js 特性 —— context.但是了解它对于了解接下来要讲解的 React-redux 很有好处,所以大家可以简单了解一下它的概念和作用. 在过 ...

  3. JavaScript整理

    JavaScript是脚本语言 常用对话框: alert()——警告对话框,作用是弹出一个警告对话框 confirm()——带确定和取消按钮,返回True或false prompt()——弹出一个可以 ...

  4. 关于Android发送短信获取送达报告的问题

    最近公司开发一个项目,要求app能够发送短信并获取送达报告.这本不是一个什么难题,实现这一功能的代码一搜一大把,那么这么简单的一个问题,为什么我要在这里提出来呢?那是因为我在写代码的时候掉入了一个坑, ...

  5. Js学习文件上传

    // 文件上传 jQuery(function() { var $ = jQuery, $list = $('#thelist'), $btn = $('#ctlBtn'), state = 'pen ...

  6. iOS 获取真机上系统动态库文件

    iOS 获取真机上所有系统库文件 系统动态库文件存放真机地址(/System/Library/Caches/com.apple.dyld/dyld_shared_cache_arm64) 在Mac\i ...

  7. 洛谷 P1726 上白泽慧音

    题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...

  8. python * urllib_urlopen( )

    python * urllib_urlopen( ) Python urllib 库提供了一个从指定的 URL 地址获取网页数据,然后对其进行分析处理,获取想要的数据. 一.urllib模块urlop ...

  9. Elasticsearch搜索含有数字标签的处理

    {"tag_id":“12345”} 在search的时候是完全匹配,因为Elasticsearch在处理这个的过程中把“123456”字符当成一个整体的数据,因此折腾了好久就是找 ...

  10. 一个SAP开发人员的双截棍之路

    由于种种原因,Jerry最近加入了SAP成都研究院的一个演讲俱乐部,这个俱乐部主要是提高大家的英语演讲能力. 说来Jerry也是大一下期和大二上期一次性高分通过四六级考试的,但是当毕业进入SAP成都研 ...