Given a singly linked list L1→L2→⋯→Ln−1→Ln and an integer 1≤k<n, you are supposed to rearrange the links to obtain a list like Lk→Ln→Lk−1→Ln−1→⋯. For example, given L being 1→2→3→4→5→6 and k=4, you must output 4→6→3→5→2→1.
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 an integer 1≤k<n where n is the number of nodes in the linked list. 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 a positive integer no more than 105, and Nex
t is the position of the next node. It is guaranteed that there are at least two nodes on the list.
Output Specification:
For each case, output in order the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
Sample Output:
00000 4 68237
68237 6 33218
33218 3 99999
99999 5 12309
12309 2 00100
00100 1 -1
-
思路:
链表题:直接模板处理,排序+输出 -
T1 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
struct Node{
int add, data, next;
}node[maxn];
vector<int> preM, postM, ans;
int main(){
int first, n, m, tmpAdd;
scanf("%d %d %d", &first, &n, &m);
for(int i = 0; i < n; ++i){
scanf("%d", &tmpAdd);
scanf("%d %d", &node[tmpAdd].data, &node[tmpAdd].next);
node[tmpAdd].add = tmpAdd;
}
int cnt = m;
while(first != -1){
if(cnt-- > 0){
preM.push_back(node[first].add);
}
else{
postM.push_back(node[first].add);
}
first = node[first].next;
}
// for(int i = 0; i < preM.size(); ++i)
// cout << preM[i] << " ";
reverse(preM.begin(), preM.end());
reverse(postM.begin(), postM.end());
int i;
for(i = 0; i < preM.size() && i < postM.size(); ++i){
ans.push_back(preM[i]);
ans.push_back(postM[i]);
}
// cout << i;
if(i == preM.size()){
for(int j = i; j < postM.size(); ++j) ans.push_back(postM[j]);
}else if(i == postM.size()){
for(int j = i; j < preM.size(); ++j) ans.push_back(preM[j]);
}
// cout << node[0].data;
int j;
for(j = 0; j < ans.size()-1; ++j)
printf("%05d %d %05d\n", ans[j], node[ans[j]].data, node[ans[j+1]].add);
printf("%05d %d -1\n", ans[j], node[ans[j]].data);
return 0;
}
- T2 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
struct Node
{
int data, nex;
}node[maxn];
void Print(const vector<int> & ans)
{
if(ans.size() == 0)
{
printf("0 -1\n");
}else
{
for(int i = 0; i < ans.size(); ++i)
{
if(i < ans.size()-1) printf("%05d %d %05d\n", ans[i], node[ans[i]].data, ans[i+1]);
else printf("%05d %d -1\n", ans[i], node[ans[i]].data);
}
}
}
int main()
{
int first, n, k;
scanf("%d %d %d", &first, &n, &k);
for(int i = 0; i < n; ++i)
{
int add;
scanf("%d", &add);
scanf("%d %d", &node[add].data, &node[add].nex);
}
int head = first, idex = k;
vector<int> ans[3];
while(head != -1)
{
if(idex-- > 0){
ans[1].push_back(head);
}else
{
ans[2].push_back(head);
}
head = node[head].nex;
}
int i, j, len1 = ans[1].size(), len2 = ans[2].size();
for(i = 0, j = 0; i < len1 && j < len2; ++i, ++j)
{
ans[0].push_back(ans[1][len1 - 1 - i]);
ans[0].push_back(ans[2][len2 - 1 - j]);
}
while(i < ans[1].size())
{
ans[0].push_back(ans[1][len1 - 1 - i]);
i++;
}
while(j < ans[2].size())
{
ans[0].push_back(ans[2][len2 - 1 - j]);
j++;
}
Print(ans[0]);
return 0;
}