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;
}
相关推荐
地平线开发者19 分钟前
征程 6X Camera 接入数据评估
算法·自动驾驶
Storynone21 分钟前
【Day23】LeetCode:455. 分发饼干,376. 摆动序列,53. 最大子序和
python·算法·leetcode
小付同学呀39 分钟前
C语言学习(八)——C判断(switch语句)
c语言·学习·算法
zhojiew1 小时前
为agent实现渐进式Skills能力的思考和实践
linux·python·算法
ATAOL1 小时前
数据结构一
数据结构·算法
zyq99101_12 小时前
Python日期处理实战代码
python·算法·蓝桥杯
罗超驿3 小时前
Java数据结构_链表
java·数据结构·链表
小璐资源网3 小时前
C++中如何正确区分`=`和`==`的使用场景?
java·c++·算法
N1_WEB3 小时前
HDU:杭电 2018 复试真题汇总
算法
AMoon丶3 小时前
C++模版-函数模版,类模版基础
java·linux·c语言·开发语言·jvm·c++·算法