-->
当前位置:首页 > 题库

PROGRAMMING:Rearrange linked list

Luz5年前 (2021-05-10)题库462
Given a single linked table $$l_ 1$$→$$L_ 2$$→$$\cdots$$→$$L_{ n-1}$$→$$L_ N $$, please write a program to rearrange the linked list to $$L_ n$$→$$L_ 1$$→$$L_{ n-1}$$→$$L_ 2$$→$$\cdots$$。 For example: given that $$l $$is 1 → 2 → 3 → 4 → 5 → 6, the output should be 6 → 1 → 5 → 2 → 4 → 3.
###Input format:
Each input contains one test case. The first line of each test case gives the address of the first node and the total number of nodes, that is, the positive integer $$n $$($$le10 ^ 5 $$). The address of the node is a 5-bit non negative integer, and the null address is represented by $$- 1 $.
Next, there are $$n $$lines, each in the following format:
```
Address Data Next
```
Where 'address' is the node address` Data 'is the data saved by the node, which is a positive integer no more than $$10 ^ 5 $` Next 'is the address of the next node. The title guarantees that there are at least two nodes on the given list.
###Output format:
For each test case, the rearranged result list is output in sequence. Each node on the list occupies a row, and the format is the same as the input.
###Input example:
```in
00100 6
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
```
###Output example:
```out
68237 6 00100
00100 1 99999
99999 5 12309
12309 2 00000
00000 4 33218
33218 3 -1
```






answer:If there is no answer, please comment