1074. Reversing Linked List (25)

简介: #include #include #include using namespace std;struct node { int address, data, next;};const int NUM...
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct node {
    int address, data, next;
};

const int NUM = 100001;
node nodes[NUM];
vector<node> list;

int main(){
    int fnAdress, n, k;
    cin >> fnAdress >> n >> k;

    for (int i = 0; i < n; i++) {
        node nodet;
        cin >> nodet.address >> nodet.data >> nodet.next;
        nodes[nodet.address] = nodet;//将每一个node放入正确的位置
    }

    int address = fnAdress;
    while (address != -1) {//去噪
        list.push_back(nodes[address]);
        address = nodes[address].next;
    }

    int size = (int)list.size();
    int round = size / k;
    for (int i = 0; i < round; i++) {
        int start = i * k;
        int end = (i + 1) * k;
        reverse(list.begin() + start, list.begin() + end);
    }
    for(int i = 0; i < size - 1; i++){
        printf("%05d %d %05d\n", list[i].address, list[i].data, list[i+1].address);
    }

    printf("%05d %d %d\n", list[size - 1].address, list[size - 1].data, -1);

    return 0;
}
AI 代码解读
目录
打赏
0
0
0
0
1
分享
相关文章
链表(Linked List)详解
链表(Linked List)详解
128 0
【PAT甲级 - C++题解】1133 Splitting A Linked List
【PAT甲级 - C++题解】1133 Splitting A Linked List
108 0
【PAT甲级 - C++题解】1074 Reversing Linked List
【PAT甲级 - C++题解】1074 Reversing Linked List
104 0
二叉树(Binary Tree)的二叉链表(Binary Linked List)实现
二叉树(Binary Tree)的二叉链表(Binary Linked List)实现
线性表的链式存储结构 单链表(Singly Linked List) C++
线性表的链式存储结构 单链表(Singly Linked List) C++
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问