1074. Reversing Linked List (25)
来源:互联网 发布:vscode 导出配置 编辑:程序博客网 时间:2024/06/05 02:07
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 400000 4 9999900100 1 1230968237 6 -133218 3 0000099999 5 6823712309 2 33218Sample Output:
00000 4 3321833218 3 1230912309 2 0010000100 1 9999999999 5 6823768237 6 -1
//1074#include<iostream>#include<vector>using namespace std;int main(){int start,n,k;int next[100000];//next[i] is the next node's address of the node with address iint val[100000];//val[i] is the value of the node with address iscanf("%d%d%d",&start,&n,&k);for(int i=0;i<n;++i){int address;scanf("%d",&address);scanf("%d%d",val+address,next+address);}int cur=start;//current nodevector<int>list;while(cur!=-1){list.push_back(cur);cur=next[cur];}//recalculate the length of list(some input node may not in the list)n=list.size();//case 6int j;for(int i=0;i<n/k;++i){for(j=(i+1)*k-1;j>i*k;--j)printf("%05d %d %05d\n",list[j],val[list[j]],list[j-1]);if(n%k==0 && i==n/k-1){//when j is unfortually the last nodeprintf("%05d %d -1\n",list[j],val[list[j]]);return 0;}else if(i==n/k-1)//if this is the last reverse partprintf("%05d %d %05d\n",list[j],val[list[j]],list[(i+1)*k]);elseprintf("%05d %d %05d\n",list[j],val[list[j]],list[(i+2)*k-1]);}for(int i=(n/k)*k;i<n-1;++i)printf("%05d %d %05d\n",list[i],val[list[i]],list[i+1]);printf("%05d %d -1\n",list[n-1],val[list[n-1]]);//the last nodereturn 0;}
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- 1074. Reversing Linked List (25)
- jasperreport linux 中文不能显示的解决方法
- Delegate,Action,Func,Predicate的使用与区别
- JavaScript前端编码Java后端解码
- 1073. Scientific Notation (20)
- UVA 106 Fermat vs. Pythagoras(勾股数)
- 1074. Reversing Linked List (25)
- H2数据库攻略之三-结合Maven
- Wince stepldr代码解析
- 欢迎使用CSDN-markdown编辑器
- CSS规范
- Android 插件化技术 加载任意未安装apk
- 黑马程序员--04C预处理
- NYOJ 374 弹球II
- 1075. PAT Judge (25)