1074 Reversing Linked List 25

cpp 复制代码
#include <cstdio>
const int maxn = 100010;
struct Node{
    int address, data, next;
}nodes[maxn];

int main() {
    int head,n,k;
    scanf("%d%d%d", &head, &n, &k);
    int add;
    for(int i = 0; i < n; i++){
        scanf("%d", &add);
        scanf("%d%d", &nodes[add].data, &nodes[add].next);
        nodes[add].address = add;
    }
    //有效节点数目
    int v = head;
    int num = 1;
    while(nodes[v].next != -1){
        num++;
        v = nodes[v].next;
    }

    
    int heads[maxn];
    int tails[maxn];
    
    int next;
    int pre = head;
    int cur = nodes[head].next;
    int curHead = head;
    int times = num / k;
    for(int i = 0; i < times; i++){
        tails[i] = curHead;
        int s = 1;
        while(s < k){
            next = nodes[cur].next;
            nodes[cur].next = pre;
            pre = cur;
            cur = next;
            s++;
        }
        heads[i] = pre;
        curHead = cur;
        pre = cur;
        cur = nodes[cur].next;
    }
    nodes[tails[times-1]].next = curHead;
    for(int i = 0; i < times-1; i++){
        nodes[tails[i]].next = nodes[heads[i+1]].address;
    }
    int ans = heads[0];
    while(nodes[ans].next != -1){
        printf("%05d %d %05d\n", nodes[ans].address, nodes[ans].data, nodes[ans].next);
        ans = nodes[ans].next;
    }
    printf("%05d %d -1\n", nodes[ans].address, nodes[ans].data);
    return 0;
}
相关推荐
J先生x31 分钟前
【IP101】图像处理进阶:从直方图均衡化到伽马变换,全面掌握图像增强技术
图像处理·人工智能·学习·算法·计算机视觉
爱coding的橙子3 小时前
每日算法刷题 Day3 5.11:leetcode数组2道题,用时1h(有点慢)
算法·leetcode
?abc!8 小时前
缓存(5):常见 缓存数据淘汰算法/缓存清空策略
java·算法·缓存
BioRunYiXue8 小时前
一文了解氨基酸的分类、代谢和应用
人工智能·深度学习·算法·机器学习·分类·数据挖掘·代谢组学
jiunian_cn9 小时前
【c++】异常详解
java·开发语言·数据结构·c++·算法·visual studio
工藤新一¹10 小时前
蓝桥杯算法题 -蛇形矩阵(方向向量)
c++·算法·矩阵·蓝桥杯·方向向量
Levin__NLP_CV_AIGC10 小时前
解决pip安装PyPI默认源速度慢
算法·pip
康康这名还挺多10 小时前
鸿蒙HarmonyOS list优化一: list 结合 lazyforeach用法
数据结构·list·harmonyos·lazyforeach
Helibo4410 小时前
GESPC++六级复习
java·数据结构·算法
EnticE15211 小时前
[高阶数据结构]二叉树经典面试题
数据结构·算法·面试