天天看点

PAT 1074. Reversing Linked List (25)

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:

sample output:

PAT 1074. Reversing Linked List (25)
PAT 1074. Reversing Linked List (25)

view code

继续阅读