当前位置:网站首页>02-线性结构3 Reversing Linked List
02-线性结构3 Reversing Linked List
2022-07-17 22:49:00 【疯疯癫癫才自由】
02-线性结构3 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:
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 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
分析:
对于前rev-1趟反转链表,每一趟的反转链表的最后一个节点的下一个地址都是后一趟的反转链表的
第一个地址;
对于res趟反转链表,如果n%k==0,则最后一个结点的下一个地址是-1,否则最后一个节点的下一个
地址是最后不能构成一个反转链表的第一个地址。
/**
分析:
对于前rev-1趟反转链表,每一趟的反转链表的最后一个节点的下一个地址都是后一趟的反转链表的
第一个地址;
对于res趟反转链表,如果n%k==0,则最后一个结点的下一个地址是-1,否则最后一个节点的下一个
地址是最后不能构成一个反转链表的第一个地址。
*/
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
struct Node
{
int addr,data,next;
int flag;
Node()
{
flag=2*maxn;
}
};
bool cmp(Node &a,Node &b)
{
return a.flag<b.flag;
}
int main()
{
int head,n,k;
scanf("%d%d%d",&head,&n,&k);
Node node[maxn];
// for(int i=0;i<maxn;++i)
// node[i].flag=0;
int addr;
for(int i=0;i<n;++i)
{
scanf("%d",&addr);
scanf("%d%d",&node[addr].data,&node[addr].next);
node[addr].addr=addr;
}
int i=0;
while(head!=-1)
{
node[head].flag=++i;
head=node[head].next;
}
n=i; //存在多余结点不在链表上的情况
sort(node,node+maxn,cmp);
/**
cout << "jidu\n";
for(int i=0;i<n;++i)
printf("%05d %d %05d\n",node[i].addr,node[i].data,node[i].next);
cout << "lo\n";
*/
int rev=n/k;
for(int i=0;i<rev;++i)
{
for(int j=k-1;j>=0;--j)
{
if(i!=rev-1) //如果是反转链表的最后一趟
{
int index=i*k+j;
if(j==0) //如果是每一趟的最后一个结点
printf("%05d %d %05d\n",node[index].addr,node[index].data,node[(i+2)*k-1].addr);
else
printf("%05d %d %05d\n",node[index].addr,node[index].data,node[index-1].addr);
}
else
{
int index=i*k+j;
if(j==0)
{
if(n%k==0)
printf("%05d %d -1\n",node[index].addr,node[index].data);
else
printf("%05d %d %05d\n",node[index].addr,node[index].data,node[(i+1)*k].addr);
}
else
printf("%05d %d %05d\n",node[index].addr,node[index].data,node[index-1].addr);
}
}
}
for(int i=rev*k;i<n;++i) //剩下不能构成一个反转链表的结点
{
if(i==n-1)
printf("%05d %d -1\n",node[i].addr,node[i].data);
else
printf("%05d %d %05d\n",node[i].addr,node[i].data,node[i+1].addr);
}
return 0;
}
边栏推荐
- MMRotate从零开始训练自己的数据集
- 1. Basic concepts of DBMS
- 【花雕动手做】有趣好玩的音乐可视化项目(11)---WS2812幻彩灯带
- MySQL installation
- GYM103660E. Disjoint path on tree count
- 最大堆与堆排序和优先队列
- PKI:TLS握手
- UCAS. Deep learning Final examination questions and brief thinking analysis
- PCIe Cameralink signal generator (Cameralink image analog source)
- Maximum heap and heap sort and priority queue
猜你喜欢
随机推荐
vscod
MySQL installation
UCAS. Deep learning Final examination questions and brief thinking analysis
微信小程序6-云开发-云数据库
买股票开户应该选哪个证券公司?什么证券公司是更安全的
[code attached] how to realize handwritten digit recognition with hog+svm
Unix ls
Summary of the third week of summer vacation
ORA-08103
TDesign compositionapi refactoring path
C - Matrix Chain Multiplication(栈的应用)
Single channel 6Gsps 12 bit AD acquisition and single channel 6Gsps 16 bit Da (ad9176) output sub card based on vita57.1 standard
Wechat applet 9 release code
Top domestic experts gathered in Guangzhou to discuss the safety application of health care data
MMRotate从零开始训练自己的数据集
How to bind process threads to CPU cores
A - Play on Words
Chang'an chain learning research - storage analysis wal mechanism
Unix ls
3438. 数制转换









