标签:
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
思路:这道题我花了好长时间去解析,为什么花了好长时间, 首先没有读懂题,其次在没读懂题的基础上纠结了好长时间就是临时变量的问题。最后再读懂题的基础上没有考虑边界的情况。总之:受益匪浅
1:这个是错误的代码: 错误的代码必须一辈子铭记。
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 #define MAX 100010 5 struct Info 6 { 7 int pre; 8 int data; 9 int next; 10 }node[MAX]; 11 //一定要仔细读题....every k elements 12 int main(int argc, char *argv[]) 13 { 14 int start,N,K; 15 scanf("%d%d%d",&start,&N,&K); 16 for(int i=0;i<N;i++) 17 { 18 int pre,data,next; 19 scanf("%d%d%d",&pre,&data,&next); 20 node[pre].data=data; 21 node[pre].next=next; 22 } 23 int pt=start; 24 //进行前驱记录 25 node[pt].pre=-2;//开头的前驱代表着开始 26 while(node[pt].next!=-1) 27 { 28 int temp=pt; 29 pt=node[pt].next; 30 node[pt].pre=temp; 31 } 32 //寻找第K个 33 pt=start; 34 for(int i=1;i<K&&node[pt].next!=-1;i++) 35 { 36 pt=node[pt].next; 37 } 38 //进行反转 39 int temp=node[pt].next; 40 start=pt; 41 while(node[pt].pre!=-2) 42 { 43 node[pt].next=node[pt].pre; 44 pt=node[pt].pre; 45 } 46 node[pt].next=temp; 47 //进行结果的输出 48 pt=start; 49 while(node[pt].next!=-1) 50 { 51 printf("%05d %d %05d\n",pt,node[pt].data,node[pt].next); 52 pt=node[pt].next; 53 } 54 printf("%05d %d %d\n",pt,node[pt].data,node[pt].next); 55 return 0;
2:正确的代码
1 #include <cstdio> 2 #include <vector> 3 using namespace std; 4 #define MAX 100010 5 struct node 6 { 7 int add; 8 int data; 9 int next; 10 }; 11 int main() 12 { 13 vector<node>in(MAX); //像数组一样 14 vector<node>srt; 15 vector<node>out; 16 int start,N,K; 17 scanf("%d%d%d",&start,&N,&K); 18 for(int i=0;i<N;i++) 19 { 20 node temp; 21 scanf("%d%d%d",&temp.add,&temp.data,&temp.next); 22 in[temp.add]=temp; 23 } 24 int next=start; 25 while(next!=-1) 26 { 27 srt.push_back(in[next]); 28 next=in[next].next; 29 } 30 int left=0; 31 while(left<srt.size()) 32 { 33 34 for(int i=left+K-1;i>=left;i--) 35 { 36 if(i>=srt.size()) 37 break; 38 out.push_back(srt[i]); 39 } 40 left+=K; 41 } 42 if(left!=srt.size()) 43 left-=K; 44 while(left<srt.size()) 45 { 46 out.push_back(srt[left]); 47 left++; 48 } 49 /* 50 int len=srt.size(); 51 int right=K-1; 52 while(right<len) 53 { 54 for(int i=right;i>right-K;i--) 55 { 56 out.push_back(srt[i]); 57 } 58 right+=K; 59 } 60 right-=K; 61 right++; 62 for(int i=right;i<srt.size();i++) 63 { 64 out.push_back(srt[i]); 65 }*/ 66 for(int i=0;i<out.size();i++) 67 { 68 if(i==out.size()-1) 69 printf("%05d %d -1\n",out[i].add,out[i].data); 70 else 71 printf("%05d %d %05d\n",out[i].add,out[i].data,out[i+1].add); 72 } 73 return 0; 74 }
PAT1074. Reversing Linked List
标签:
原文地址:http://www.cnblogs.com/GoFly/p/4298956.html