当前位置:网站首页>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;
}
边栏推荐
- Damn it, why is there less space on the USB flash drive? It's the EFI partition. Delete it
- 009 execution sequence of SQL statement of interview questions
- 2. MySQL introduction
- Which securities company should I choose to open a stock account? What securities company is safer
- 现场可程式化逻辑闸阵列 FPGA
- State machine exercise
- Li Hongyi machine learning 2022.7.15 -- gradient descent
- 国内顶尖专家集聚广州,探讨健康医疗数据安全应用
- Chapter 1 preliminary knowledge
- Unix ls
猜你喜欢
微信小程序8-云函数
MySQL CPU usage is soaring. How to locate who is occupying it
Leetcode 1275. 找出井字棋的獲勝者
UCAS. Deep learning Final review knowledge points summary notes
Top domestic experts gathered in Guangzhou to discuss the safety application of health care data
Li Hongyi machine learning 2022.07.15 -- error
3U VPX cooling conduction high performance srio/ Ethernet data exchange board
session management
Leetcode 1296. Divide the array into a set of consecutive numbers (solved)
Wechat applet 8 cloud function
随机推荐
Performance design of distributed transaction
Maximum heap and heap sort and priority queue
Leetcode 1296. Divide the array into a set of consecutive numbers (solved)
Unix ls
【Renesas RA6M4开发板之I2C读取mpu6050】
Behind the high salary of programmers' operation and maintenance
PKI:TLS握手
Internship is a barrier to enter the society
PCIe Cameralink signal generator (Cameralink image analog source)
测试图片
背包问题 (Knapsack problem)
Istio XDS配置生成实现
【微服务】 微服务学习笔记三:利用Feign替换RestTemplate完成远程调用
Zabbix实现对Redis的监控
UVA - 12096 The SetStack Computer
Distributed transaction summary
Tianqin Chapter 9 after class exercise code
2021.07.13【B站】是这样崩的
Scheduled tasks, VIM directly creates and modifies users
07_服务注册与发现总结