Given a constant k and a singly linked list L, you is 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 the one test case. The first line contains the address of the first node, a positive N (<=) which are the total number O F nodes, and a positive K (<=n) which are 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 was 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 33218
Sample Output:
00000 4 3321833218 3 1230912309 2 0010000100 1 9999999999 5 6823768237 6-1
Thinking: This problem I spent a long time to parse, why spent a long time, first did not read the problem, and then the foundation of the lack of reading and the struggle for a long time is the temporary variable. The final reading of the question on the basis of no consideration of the boundary situation. In short: Benefit
1: This is the wrong code: the wrong code must be remembered for a lifetime.
1#include <iostream>2#include <cstdio>3 using namespacestd;4 #defineMAX 1000105 structInfo6 {7 intPre; 8 intdata; 9 intNext;Ten }node[max]; One //Be sure to read the question carefully .... every k elements A intMainintargcChar*argv[]) - { - intstart,n,k; thescanf"%d%d%d",&start,&n,&K); - for(intI=0; i<n;i++) - { - intPre,data,next; +scanf"%d%d%d",&pre,&data,&next); -Node[pre].data=data; +node[pre].next=Next; A } at intpt=start; - //record the precursor -node[pt].pre=-2;//the beginning of the precursor represents the beginning - while(node[pt].next!=-1) - { - inttemp=pt; inpt=Node[pt].next; -Node[pt].pre=temp; to } + //looking for the first K -pt=start; the for(intI=1; i<k&&node[pt].next!=-1; i++) * { $pt=Node[pt].next;Panax Notoginseng } - //to reverse the inttemp=Node[pt].next; +start=pt; A while(node[pt].pre!=-2) the { +node[pt].next=Node[pt].pre; -pt=Node[pt].pre; $ } $node[pt].next=temp; - //output of the results -pt=start; the while(node[pt].next!=-1) - {Wuyiprintf"%05d%d%05d\n", pt,node[pt].data,node[pt].next); thept=Node[pt].next; - } Wuprintf"%05d%d%d\n", pt,node[pt].data,node[pt].next); - return 0;
View Code
2: The correct code
1#include <cstdio>2#include <vector>3 using namespacestd;4 #defineMAX 1000105 structnode6 {7 intadd;8 intdata;9 intNext;Ten }; One intMain () A { -Vector<node>inch(MAX);//like an array -Vector<node>SRT; theVector<node> out; - intstart,n,k; -scanf"%d%d%d",&start,&n,&K); - for(intI=0; i<n;i++) + { - node temp; +scanf"%d%d%d",&temp.add,&temp.data,&temp.next); A inch[temp.add]=temp; at } - intnext=start; - while(next!=-1) - { -Srt.push_back (inch[next]); -next=inch[Next].next; in } - intleft=0; to while(left<srt.size ()) + { - the for(inti=left+k-1; i>=left;i--) * { $ if(i>=srt.size ())Panax Notoginseng Break; - out. Push_back (Srt[i]); the } +left+=K; A } the if(left!=srt.size ()) +left-=K; - while(left<srt.size ()) $ { $ out. Push_back (Srt[left]); -left++; - } the /* - int len=srt.size ();Wuyi int right=k-1; the While (Right<len) - { Wu for (int i=right;i>right-k;i--) - { About Out.push_back (Srt[i]); $ } - right+=k; - } - right-=k; A right++; + for (int i=right;i<srt.size (); i++) the { - Out.push_back (Srt[i]); $ }*/ the for(intI=0;i< out. Size (); i++) the { the if(i== out. Size ()-1) theprintf"%05d%d-1\n", out[I].add, out[I].data]; - Else inprintf"%05d%d%05d\n", out[I].add, out[I].data, out[i+1].add); the } the return 0; About}
View Code
PAT1074. Reversing Linked List