"Zhejiang University patest" 02-1. Reversing Linked List

Source: Internet
Author: User

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
"Solution"
At first glance test instructions, very simple and easy to understand. (Don't be fooled by appearances.)
Linked list, also manually gave the node address, and began to write.
After I have experienced a toss-up, I think this problem is a table examining, because it is really to put this point with the appearance of hiding very well. It is not difficult to realize it after you have figured it out.

Say my "mental journey":
Soon found that the key to the problem is "paragraph and paragraph" between the "address" connection to be handled well.
At first, the structure was deliberately constructed to construct the node, three members, data, addr (own address), next (the next node address), where the address is stored in a string, and then create an array of elements that are structs (it is emphasized that the physical address is adjacent to a block of memory).
Naturally, I started to construct a string with the input data. How to construct it? The address is a string, and it's natural to start violently looking for the next node, apparently an O (n^2). Then think, I wipe, will not be too. Is it optimized? For a moment it was not thought (related to the first-in thinking of the string memory address).
Then think, alas this is the second in a problem, test data should not be very pit bar. (In fact, I have fallen into the hole t_t)

Then write a test, a point timeout, a point unexpectedly also WA! Time-out is understandable, and there are wa!.
Then start with the problem of WA (because the timeout may also be due to some errors in the program). Look at the code again, the boundary situation should be no problem ah.
In the discussion area by Chen grandmother reminds, the input data node unexpectedly may not be effective need to output the chain! That is, the last output chain length may not have n! Because there are invalid nodes in the input! t_t
Granny (The question) is really thoughtful! Wry smile

Then WA solved it, but there was a timeout.
It seems that the problem is on the construction chain of O (n^2).
Looking at the code again, it may be that the copy of the address copied to the string operation affected the efficiency, it is finally beginning to think of the address with an integer to store.
Immediately change the address to an integer type, or timeout.
Ask grandma, Grandma said is a little skill, the complexity is O (n).
It's O (n)!! I began to think seriously about optimization.
Do you want to sort? Order the address and then two points to find it? Even so is O (Nlogn) Ah. And the second Zhou Yi, not yet said, not so complicated. [Smile again]
At first, it has been confined to the idea of the address as a string, even thinking of the hash table, priority queue, complete binary heap, and so on data structure. But this is the second week! [Loud smile and self-deprecating]
Probably is accustomed to hit the table for most of the problem is certainly not the standard solution, the reason is that the scope of the topic data is generally large, slowly forget the "beginner's mind". [Introduction of deception and a wry smile!] ]
This problem is really hidden very well, granny really heart.

Thinking experience a wave of inscrutable, finally think of the address as an array of numerical subscript, and the array value is the data content and the next node address this is what I call "play table" approach.
Can't help but a sigh of regret ah t_t

There are a few things to say:
1, do not underestimate any problem;
2, this problem really cleverly revealed the list and vector of the difference and essence, seemingly the topic is a list, actually do it is a vector;
3, in the code, the output leading 0 format control:%05d. Right alignment, at least 5 bits, less than 0 to fill.

The AC code is as follows and may be spatially optimized, as I do not make drastic changes in order to be able to change the original version of the code as much as possible:
1#include <stdio.h>2#include <malloc.h>3 4 #defineAddrmax 10000045 6typedefstructAnsnode7 {8     intaddr, data;9}anode, *Panode;Ten  One intMainvoid) A { -     intN, K, I, J, block, rest, top =0; -     intstart, temp; the      -scanf" %d%d%d", &start, &n, &k); -      -     int* Data = (int*) malloc (sizeof(int)*(Addrmax)); +     int* Next = (int*) malloc (sizeof(int)*(Addrmax)); -      +Panode ans = (panode) malloc (sizeof(anode) * (n+1)); A      at      for(i =0; I < n; i++) -     { -scanf"%d", &temp); -scanf"%d%d", &data[temp], &next[temp]); -     } -      in     //construct the chain by address and put it into ans. -      while(Start! =-1) to     { +Ans[top].data =Data[start]; -ANS[TOP].ADDR =start; theStart =Next[start]; *top++; $     }Panax Notoginsengn =top; -      the     //once per k segment output, and processing the last time less than K, the key point is the "address" between "segment and segment" Stitching +block = n/k; Rest = n%K; A      for(j =0; J < Block; J + +) the     { +          for(i = (j +1) *k-1; i > j*k; i--) -         { $printf"%05d%d%05d\n", Ans[i].addr, Ans[i].data, ans[i-1].addr); $         } -printf"%05d%d", ANS[I].ADDR, ans[i].data); -         if(Rest = =0) the         { -             if(j = Block-1) printf ("-1");Wuyi             Elseprintf"%05d", ans[(j+2) *k-1].addr); the}Else -         { Wu             if(j = Block-1) printf ("%05d", ans[(j+1)*k].addr); -             Elseprintf"%05d", ans[(j+2) *k-1].addr); About         } $printf"\ n"); -     } -     if(Rest! =0) -     { A          for(i = block*k; I < n-1; i++) printf ("%05d%d%05d\n", Ans[i].addr, Ans[i].data, ans[i+1].addr); +printf"%05d%d-1\n", ANS[I].ADDR, ans[i].data); the     } -      $     return 0; the}

"Zhejiang University patest" 02-1. Reversing Linked List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.