牛客网_PAT乙级_1015反转链表 (25)【没做出来】
題目描述
給定一個常數K以及一個單鏈表L,請編寫程序將L中每K個結點反轉。例如:給定L為1→2→3→4→5→6,K為3,則輸出應該為
3→2→1→6→5→4;如果K為4,則輸出應該為4→3→2→1→5→6,即最后不到K個元素不反轉。
輸入描述:
每個輸入包含1個測試用例。每個測試用例第1行給出第1個結點的地址、結點總個數正整數N(<= 105)、以及正整數K(<=N),即要求反轉的
子鏈結點的個數。結點的地址是5位非負整數,NULL地址用-1表示。
接下來有N行,每行格式為:
Address Data Next
其中Address是結點地址,Data是該結點保存的整數數據,Next是下一結點的地址。
輸出描述:
對每個測試用例,順序輸出反轉后的鏈表,其上每個結點占一行,格式與輸入相同。
示例1
輸入
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
輸出
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
別人的思路
之前一直想著用最小的空間解決問題,但一直超時,思維也一直被局限在這里,后來看了別人的答案才恍然大悟——原來自己鉆牛角尖了。。。思路是:利用足夠大的數組作為一個靜態鏈表,結點的地址是數組下標,這樣對鏈表的操作就很簡單了,也不用排序;每k個結點翻轉時,將這k個元素組成的鏈表拆下來,鏈表中剩余的部分組成一個鏈表,然后將這k個元素以頭插法插入到該鏈表中,為了操作方便,將鏈表設為帶頭結點的鏈表。要注意的是對每k個結點翻轉的循環次數的控制,這里也是個坑,因為輸入時有的結點是無效的,并不在鏈表中,所以鏈表的元素個數要自行統計。
別人的代碼
#include <stdio.h> #include <memory.h>#define LIMIT 100002struct linknode {int data;int next; };int reversek(struct linknode *, int, int);int main(void) {struct linknode list[LIMIT];int faddr, n, k;int addr, data, next;int i, j, count = 0;scanf("%d %d %d", &faddr, &n, &k);memset(list, 0, LIMIT * sizeof(struct linknode));list[LIMIT-1].next = faddr; //head nodefor(i = 0; i < n; ++i) {scanf("%d %d %d", &addr, &data, &next);list[addr].data = data;list[addr].next = next;}for(i = list[LIMIT-1].next; i != -1; i = list[i].next)++count; //count valid nodesfor(j = count / k, i = LIMIT - 1; j > 0 && i != -1; --j)i = reversek(list, i, k); //reverse every k nodesfor(i = list[LIMIT-1].next; list[i].next != -1; i = list[i].next)printf("%05d %d %05d\n", i, list[i].data, list[i].next);printf("%05d %d %d\n", i, list[i].data, list[i].next);return 0; }int reversek(struct linknode *list, int head, int k) {int i, j, p;int nexthead;for(j = 0, p = list[head].next; j < k && p != -1; ++j) //find tailp = list[p].next;nexthead = list[head].next;list[head].next = p;for(j = 0, i = nexthead; j < k && i != -1; ++j) {p = list[i].next;list[i].next = list[head].next; //insert at the headlist[head].next = i;i = p;}return nexthead; }我的代碼(問題出在:雙向鏈表的pre和next在翻轉后會混在一起,無法遍歷打印,而且翻轉的過程有錯誤,沒改完)
除非“假翻轉”,用大哥走4步,小弟回溯打印3步的思想(能夠回溯,這也是用雙向鏈表存儲的原因)
#include<iostream> #include<string> #include<vector> class DoubleNode { public:int address;int data;int pre;int next;void putIn(int address, int data, int pre, int next);//構造器 }; void DoubleNode::putIn(int address, int data, int pre, int next) {this->address = address;this->data = data;this->pre = pre;this->next = next; } void reverse(DoubleNode* nodeInOrder, int dage, int reverseNum)//找到第一個和最后一個,改變指針指向,不需要改變元素的值 {int first = dage;int last = dage + reverseNum - 1;if (first == 0)//如果是頭{nodeInOrder[last].next = -1;//last的next指向-1nodeInOrder[nodeInOrder[last].next].pre = nodeInOrder[first].address;//last后面的元素的pre指向firstnodeInOrder[first].next = nodeInOrder[nodeInOrder[last].next].address;//first的next指向last后面的元素}else if (nodeInOrder[last].next == -1)//如果是尾{nodeInOrder[nodeInOrder[first].pre].next = nodeInOrder[last].address;//first前面的元素的next指向lastnodeInOrder[last].pre = nodeInOrder[nodeInOrder[first].pre].address;//last的pre指向first前面的元素nodeInOrder[first].next = -1;//first的next指向-1}else{nodeInOrder[nodeInOrder[first].pre].next = nodeInOrder[last].address;//first前面的元素的next指向lastnodeInOrder[last].pre = nodeInOrder[nodeInOrder[first].pre].address;//last的pre指向first前面的元素nodeInOrder[nodeInOrder[last].next].pre = nodeInOrder[first].address;//last后面的元素的pre指向firstnodeInOrder[first].next = nodeInOrder[nodeInOrder[last].next].address;//first的next指向last后面的元素} }int main() {//輸入第一行int headAddress;//第1個結點的地址int total;//結點總個數int reverseNum;//要求反轉的子鏈結點的個數std::cin >> headAddress >> total >> reverseNum;if (reverseNum > total)reverseNum = total;//輸入后面所有行int address;int next;int data;//放進巨大的vectorstd::vector <DoubleNode> nodeInOrder(100000);int t;int preAddress = -1;for (t = 0; t < total; t++){std::cin >> address >> data >> next;nodeInOrder[address].putIn(address, data, 0, next);}//填充pre指針、統計有效個數int count = 1;nodeInOrder[headAddress].pre = -1;//第一項的pre是-1int curAddress = headAddress;//從第一項開始for (; nodeInOrder[curAddress].next != -1;){nodeInOrder[nodeInOrder[curAddress].next].pre = nodeInOrder[curAddress].address;//后一項的pre存儲的是前一項的地址curAddress = nodeInOrder[curAddress].next;//cur指向下一個地址count++;}//翻轉int dage;//每次走reverseNum步for (dage = 0; dage*reverseNum - 1 <= count; dage++){reverse(&nodeInOrder[0], dage, reverseNum);}//輸出(todo)system("pause"); }部分測試用例
30201 100000 16284
00000 9343 00001
00001 4716 00002
00002 1073 00003
00003 19447 00004 00004 21871 00005 00005 30688 00006 00006 12158 00007 00007 11270 00008 00008 30157 00009 00009 9063 00010 00010 30552 00011 00011 23427 00012 00012 2936 00013 00013 1069 00014 00014 20583 00015 00015 18212 00016 00016 3475 00017 00017 14522 00018 00018 16355 00019 00019 32252 00020 00020 21882 00021 00021 26837 00022 00022 6487 00023 00023 22381 00024 00024 29768 00025 00025 12644 00026 00026 22704 00027 00027 17384 00028 00028 13916 00029 00029 27364 00030 00030 10049 00031 00031 17621 00032 00032 13037 00033 00033 3588 00034 00034 22694 00035 00035 32072 00036 00036 16915 00037 00037 4342 00038 00038 10872 00039 00039 23091 00040 00040 2344 00041 00041 14630 00042 00042 28156 00043 00043 30204 00044 00044 30146 00045 00045 8283 00046 00046 26085 00047 00047 21461 00048 00048 26129 00049 00049 6965 00050 00050 29578 00051 00051 6833 00052 00052 27260 00053 00053 29695 00054 00054 4466 00055 00055 23650 00056 00056 24744 00057 00057 19020 00058 00058 22983 00059 00059 9120 00060 00060 24049 00061 00061 14968 00062 00062 17866 00063 00063 25854 00064 00064 7355 00065 00065 26633 00066 00066 13063 00067 00067 13314 00068 00068 3479 00069 00069 17401 00070 00070 20450 00071 00071 11459 00072 00072 16888 00073 00073 10864 00074 00074 8319 00075 00075 1886 00076 00076 23663 00077 00077 11922 00078 00078 10646 00079 00079 22306 00080 00080 8086 00081 00081 14623 00082 00082 10356 00083 00083 26834 00084 00084 22827 00085 00085 21548 00086 00086 12579 00087 00087 2373 00088 00088 13794 00089 00089 12151 00090 00090 26506 00091 00091 17615 00092 00092 6483 00093 00093 15232 00094 00094 26569 00095 00095 31046 00096 00096 25534 00097 00097 31989 00098 00098 30058 00099 00099 7036 00100 00100 2435 00101 00101 31259 00102 00102 2092 00103 00103 1088 00104 00104 3516 00105 00105 20498 00106 00106 30008 00107 00107 25185 00108 00108 1786 00109 00109 6243 00110 00110 6983 00111 00111 28365 00112 00112 3214 00113 00113 31439 00114 00114 11927 00115 00115 17950 00116 00116 27281 00117 00117 26345 00118 00118 282 00119 00119 2078 00120 00120 30172 00121 00121 19359 00122 00122 29087 00123 00123 7368 00124 00124 5141 00125 00125 32179 00126 00126 4107 00127 00127 18753 00128 00128 30010 00129 00129 22682 00130 00130 3013 00131 00131 22472 00132 00132 14171 00133 00133 25981 00134 00134 29806 00135 00135 25843 00136 00136 17076 00137 00137 24716 00138 00138 3598 00139 00139 15108 00140 00140 16586 00141 00141 17281 00142 00142 20078 00143 00143 17993 00144 00144 4207 00145 00145 27482 00146 00146 8223 00147 00147 22514 00148 00148 708 00149 00149 16104 00150 00150 450 00151 00151 4589 00152 00152 28557 00153 00153 16915 00154 00154 9673 00155 00155 19916 00156 00156 15484 00157 00157 1043 00158 00158 2589 00159 00159 11923 00160 00160 27465 00161 00161 14910 00162 00162 28384 00163 00163 18235 00164 00164 10569 00165 00165 1251 00166 00166 2318 00167 00167 10525 00168 00168 12621 00169 00169 29238 00170 00170 23659 00171 00171 31597 00172 00172 23184 00173 00173 10923 00174 00174 21550 00175 00175 15679 00176 00176 6899 00177 00177 29247 00178 00178 3132 00179 00179 22946 00180 00180 19900 00181 00181 31194 00182 00182 27586 00183 00183 18506 00184 00184 31629 00185 00185 4810 00186 00186 19296 00187 00187 12862 00188 00188 17670 00189 00189 10666 00190 00190 28914 00191 00191 11763 00192 00192 23492 00193 00193 12683 00194 00194 10308 00195 00195 11113 00196 00196 5692 00197 00197 16571 00198 00198 29476 00199 00199 26435 00200 00200 5844 00201 00201 16110 00202 00202 12233 00203 00203 27532 00204 00204 17883 00205 00205 13885 00206 00206 5116 00207 00207 11203 00208 00208 4153 00209 00209 18788 00210 00210 30473 00211 00211 8013 00212 00212 7896 00213 00213 7976 00214 00214 1195 00215 00215 6164 00216 00216 19161 00217 00217 10531 00218 00218 22356 00219 00219 29189 00220 00220 21696 00221 00221 3180 00222 00222 15614 00223 00223 21303 00224 00224 22211 00225 00225 17516 00226 00226 17059 00227 00227 5338 00228 00228 950 00229 00229 24283 00230 00230 5192 00231 00231 13879 00232 00232 1369 00233 00233 16706 00234 00234 3306 00235 00235 30356 00236 00236 29131 00237 00237 24133 00238 00238 15651 00239 00239 26106 00240 00240 15894 00241 00241 8601 00242 00242 27361 00243 00243 3952 00244 00244 12471 00245 00245 22220 00246 00246 1899 00247 00247 13632 00248 00248 12156 00249 00249 1499 00250 00250 7250 00251 00251 32748 00252 00252 26227 00253 00253 15800 00254 00254 14861 00255 00255 18152 00256 00256 20385 00257 00257 30925 00258 00258 28070 00259 00259 13748 00260 00260 27126 00261 00261 14367 00262 00262 1754 00263 00263 30269 00264 00264 23203 00265 00265 14749 00266 00266 32096 00267 00267 22879 00268 00268 9948 00269 00269 15138 00270 00270 22537 00271 00271 30258 00272 00272 2738 00273 00273 14777 00274 00274 20113 00275 00275 17210 00276 00276 17649 00277 00277 16432 00278 00278 7456 00279 00279 18844 00280 00280 16193 00281 00281 18901 00282 00282 30893 00283 00283 19659 00284 00284 10985 00285 00285 20707 00286 00286 2006 00287 00287 32654 00288 00288 29773 00289 00289 4481 00290 00290 12579 00291 00291 7300 00292 00292 17202 00293 00293 1975 00294 00294 12951 00295 00295 2023 00296 00296 27582 00297 00297 9372 00298 00298 5236 00299 00299 20179 00300 00300 659 00301 00301 17005 00302 00302 4415 00303 00303 19277 00304 00304 22681 00305 00305 9529 00306 00306 26873 00307 00307 29284 00308 00308 2363 00309 00309 6270 00310 00310 1034 00311 00311 26181 00312 00312 25150 00313 00313 30579 00314 00314 20407 00315 00315 10355 00316 00316 15415 00317 00317 27692 00318 00318 18842 00319 00319 17305 00320 00320 2569 00321 00321 1449 00322 00322 21701 00323 00323 22234 00324 00324 6088 00325 00325 2030 00326 00326 21343 00327 00327 9092 00328 00328 12597 00329 00329 13810 00330 00330 17809 00331 00331 15941 00332 00332 20226 00333 00333 9990 00334 00334 32397 00335 00335 27734 00336 00336 1727 00337 00337 28361 00338 00338 27904 00339 00339 9844 00340 00340 11195 00341 00341 778 00342 00342 11855 00343 00343 26784 00344 00344 2565 00345 00345 29022 00346 00346 463 00347 00347 23208 00348 00348 21129 00349 00349 23816 00350 00350 27260 00351 00351 6033 00352 00352 25561 00353 00353 24757 00354 00354 16515 00355 00355 2605 00356 00356 16520 00357 00357 24200 00358 00358 28256 00359 00359 29936 00360 00360 20335 00361 00361 5924 00362 00362 21660 00363 00363 23930 00364 00364 32281 00365 00365 23660 00366 00366 22958 00367 00367 23048 00368 00368 31827 00369 00369 22040 00370 00370 31115 00371 00371 13878 00372 00372 5877 00373 00373 14424 00374 00374 21666 00375 00375 1894 00376 00376 16775 00377 00377 5573 00378 00378 19702 00379 00379 24622 00380 00380 6280 00381 00381 16088 00382 00382 27022 00383 00383 27476 00384 00384 24701 00385 00385 28442 00386 00386 11858 00387 00387 3313 00388 00388 14170 00389 00389 9603 00390 00390 21082 00391 00391 10538 00392 00392 6763 00393 00393 5667 00394 00394 30744 00395 00395 3204 00396 00396 27775 00397 00397 9840 00398 00398 8356 00399 00399 11123 00400 00400 5244 00401 00401 23913 00402 00402 13330 00403 00403 17044
總結
以上是生活随笔為你收集整理的牛客网_PAT乙级_1015反转链表 (25)【没做出来】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客网_PAT乙级1014_科学计数法
- 下一篇: 牛客网_PAT乙级_1016程序运行时间