02-1. Reversing Linked List (25) PAT DBS打基础之练习

2024-06-12 15:18

本文主要是介绍02-1. Reversing Linked List (25) PAT DBS打基础之练习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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个元素输出一次就行, 不足k个元素直接原序输出。 此题比较坑的是第6组测试数据,输入的节点中有无效节点。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<cstring>
using namespace std;
const int mx=100005;
typedef struct node{int add;int data;int nextAdd;
}Node; 
Node nod[mx],ans[mx];
int main()
{int first,n,k;while(scanf("%d %d %d",&first,&n,&k)!=EOF){ int i=0,N=n;while(N--){scanf("%d",&i);nod[i].add=i;int t;scanf("%d",&t);nod[i].data=t;scanf("%d",&t);nod[i].nextAdd=t;		}int j=0;for(int i=first;;){ans[j].add=nod[i].add;ans[j].data=nod[i].data;j++;i=nod[i].nextAdd;if(i == -1) break;}	n=j;//处理无效结点。 int pre=0,stop=n/k*k-1;if(n%k==0) ans[n-1+k].add=-1;else ans[n-1].nextAdd=-1;//每k个结点逆序输出。 for(j=k-1;j<=stop;j+=k){int temp=j;while(temp > pre){ printf("%05d %d %05d\n",ans[temp].add,ans[temp].data,ans[temp-1].add);temp--;		}pre=j+1;if(ans[j+k].add!=-1)if(n%k!=0 && j==stop) printf("%05d %d %05d\n",ans[temp].add,ans[temp].data,ans[j+1].add);else printf("%05d %d %05d\n",ans[temp].add,ans[temp].data,ans[j+k].add);else if(n%k==0)printf("%05d %d %d\n",ans[temp].add,ans[temp].data,ans[j+k].add);}if(n%k != 0){for(i=j+1-k;i<n;i++)if(ans[i].nextAdd!=-1)printf("%05d %d %05d\n",ans[i].add,ans[i].data,ans[i+1].add);else printf("%05d %d %d\n",ans[i].add,ans[i].data,ans[i].nextAdd);}   
}return 0;
}


这篇关于02-1. Reversing Linked List (25) PAT DBS打基础之练习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1054581

相关文章

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

Python中合并列表(list)的六种方法小结

《Python中合并列表(list)的六种方法小结》本文主要介绍了Python中合并列表(list)的六种方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录一、直接用 + 合并列表二、用 extend() js方法三、用 zip() 函数交叉合并四、用

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

Java使用Stream流的Lambda语法进行List转Map的操作方式

《Java使用Stream流的Lambda语法进行List转Map的操作方式》:本文主要介绍Java使用Stream流的Lambda语法进行List转Map的操作方式,具有很好的参考价值,希望对大... 目录背景Stream流的Lambda语法应用实例1、定义要操作的UserDto2、ListChina编程转成M

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

java streamfilter list 过滤的实现

《javastreamfilterlist过滤的实现》JavaStreamAPI中的filter方法是过滤List集合中元素的一个强大工具,可以轻松地根据自定义条件筛选出符合要求的元素,本文就来... 目录1. 创建一个示例List2. 使用Stream的filter方法进行过滤3. 自定义过滤条件1. 定

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

python中列表list切分的实现

《python中列表list切分的实现》列表是Python中最常用的数据结构之一,经常需要对列表进行切分操作,本文主要介绍了python中列表list切分的实现,文中通过示例代码介绍的非常详细,对大家... 目录一、列表切片的基本用法1.1 基本切片操作1.2 切片的负索引1.3 切片的省略二、列表切分的高

java两个List的交集,并集方式

《java两个List的交集,并集方式》文章主要介绍了Java中两个List的交集和并集的处理方法,推荐使用Apache的CollectionUtils工具类,因为它简单且不会改变原有集合,同时,文章... 目录Java两个List的交集,并集方法一方法二方法三总结java两个List的交集,并集方法一

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元