02-线性结构3 Reversing Linked List(25 point(s)) 【链表】

2023-11-18

02-线性结构3 Reversing Linked List(25 point(s))

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 (≤10​5​​) 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

思路

用 STL 模拟 链表

然后 要注意
可能会有 多余结点 不在链表上面

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;

const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30;

const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;

struct Node
{
    int add;
    int value;
    int next;
    void read()
    {
        scanf("%d%d%d", &add, &value, &next);
    }
}q[maxn];

vector <Node> v, ans;

map <int, Node> m;

void dfs(int add)
{
    v.pb(m[add]);
    if (m[add].next != -1)
        dfs(m[add].next);
}

int main()
{
    int start, n, k;
    scanf("%d%d%d", &start, &n, &k);
    for (int i = 0; i < n; i++)
    {
        q[i].read();
        m[q[i].add] = q[i];
    }
    dfs(start);
    n = v.size();
    int len = v.size() / k;
    for (int i = 1; i <= len; i++)
    {
        int j = i * k - 1;
        int m = k;
        while (m--)
            ans.pb(v[j--]);
    }
    for (int i = len * k; i < n; i++)
        ans.pb(v[i]);
    for (int i = 0; i < n - 1; i++)
        printf("%05d %d %05d\n", ans[i].add, ans[i].value, ans[i + 1].add);
    printf("%05d %d -1\n", ans[n - 1].add, ans[n - 1].value);
}






本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

02-线性结构3 Reversing Linked List(25 point(s)) 【链表】 的相关文章

  • 【简单】228. 汇总区间

    原题链接 https leetcode cn problems summary ranges description 228 汇总区间 给定一个 无重复元素 的 有序 整数数组 nums 返回 恰好覆盖数组中所有数字 的 最小有序 区间范围

随机推荐

  • CVPR 2021 Object Detection

    一 关于3D有26篇 3DIoUMatch Leveraging IoU Prediction for Semi Supervised 3D Object Detection ST3D Self Training for Unsupervi
  • 数据结构---红包分配算法

    红包分配算法 错误解法 二倍均值法 JAVA实现 线段切割法 确定每一条子线段的长度 JAVA实现 问题如下 所有人抢到的金额之和要等于红包金额 不能多也不能少 每个人至少抢到1分钱 要保证红包拆分的金额尽可能分布均衡 不要出现两极分化太严
  • Linux下软链接方法切换CUDA版本

    Linux下软链接方法切换CUDA版本 Linux下安装多版本的CUDA 直接切换版本 CUDA切换 sudo rm rf usr local cuda 删除之前的软链接 sudo ln s usr local cuda 10 0 usr
  • Allegro如何导出和导入设计规则操作指导

    Allegro如何导出和导入设计规则操作指导 当需要借用另外一款PCB的设计规则时候 Allegro支持把PCB设计规则导入到另外一块PCB中 如下图 具体操作如下 打开规则管理器 打开后如下图
  • 黑马程序员mysql笔记--索引基本操作部分

    视频链接 https www bilibili com video BV1iF411z7Pu 1 3 1 索引的基本操作 1 3 1 1 索引概述 索引是什么 索引是通过某种算法 构建出一个数据模型 用于快速找出在某个列中有一特定值的行 不
  • 《A Metric Learning Reality Check》笔记

    1 是 metric learning 的一篇学术打假文 回顾了 deep metric learning 领域两个经典工作 contrastive loss triplet loss 和近年来 2017 2019 见文中 Table 6
  • DC-DC电源管理

    BUCK电源芯片的使用与选择 BUCK电路降压原理 在开关S闭合时 对电感L与电容C进行充电同时也对负载R供电 在开关S断开时储能元器件L与C继续对R进行供电并通过D1形成回路 输出电压Vo Vi Ton Ton Toff Ton 开关S闭
  • 【C语言】实现一个通讯录:通讯录可以用来存储1000个人的信息,每个人的信息包括: 姓名、性别、年龄、电话、住址

    先写一个只能录固定人数的通讯录 不能增容 不能保存 通讯录可以用来存储1000个人的信息 每个人的信息包括 姓名 性别 年龄 电话 住址 实现功能 1 添加联系人信息 2 删除指定联系人信息 3 查找指定联系人信息 4 修改指定联系人信息
  • Linux 压缩解包命令讲解

    tar命令使用讲解 压缩 tar命令 c 生成档案文件 v 列出归档解档的详细过程 f 指定档案文件名称 t 列出档案在包含的文件 x 解开档案文件 打包 tar cvf a tar txt tar cf a tar txt 解包 tar
  • mysql split函数用逗号分隔的实现方法

    本文主要介绍了mysql split函数用逗号分隔的实现 文中通过示例代码介绍的非常详细 对大家的学习或者工作具有一定的参考学习价值 需要的朋友们下面随着小编来一起学习学习吧 1 定义存储过程 用于分隔字符串 1 2 3 4 5 6 7 8
  • 服务端推送SSE技术

    SSE Server Sent Events 是一种用于实现服务器主动向客户端推送数据的技术 也被称为 事件流 Event Stream 它基于 HTTP 协议 利用了其长连接特性 在客户端与服务器之间建立一条持久化连接 并通过这条连接实现
  • 杂学日记

    glob函数的使用 glob库函数用于Linux文件系统中路径名称的模式匹配 即查找文件系统中指定模式的路径 注意 这不是正则表达式匹配 虽然有些相似 但还是有点差别 glob函数原型 include
  • 快速提升代码能力(7)sort(a,a+n);

    从零起步看算法 第七天 4 12 q9 交叉排序 算是一道水题了 1 花点时间看一下样例 就可以理解题意了 2 本题唯一知识点 用sort 实现 数组的从大到小排序 很简单的操作 https blog csdn net jimmy lee1
  • python 字典中 get 和 [] 的区别

    在Python中 get和 是用来访问字典 dict 或字典样的对象 例如defaultdict 中的元素的两种常见方式 它们有一些区别 使用get方法时 如果指定的键不存在于字典中 它会返回一个默认值 默认为None 也可以自定义 而不会
  • ConceptFusion 论文翻译

    ConceptFusion 论文翻译 本文仅仅是本人在英语水平不够的情况下对原论文的翻译 如果有错烦请指正 原始论文地址参见 https arxiv org pdf 2302 07241 pdf 下面开始正式翻译 摘要 构建三维环境地图对于
  • Wireshark网络抓包工具下载和安装(2023)

    一 Wireshark的下载 下载网址 https www wireshark org download html 也可以阿里网盘自取 wiresharkhttps www aliyundrive com s c4qeGBQMfwj提取码
  • 实现 Java 平台的三种方式

    广泛地说 只要能执行 Java bytecode 者 就可以称为 Java 平台 Java platform 大致上 实现 Java 平台的方式有三种 分别是 Java 虚拟机器 Java Virtual Machine JVM Java
  • Java实现企业微信上传临时文件获取media_id

    目录 1 上传临时素材API 2 测试代码 3 测试完成 4 遇到的问题 4 1 文件类型无法解析 1 上传临时素材API 调试工具 素材上传得到media id 该media id仅三天内有效 media id在同一企业内应用之间可以共享
  • Qt之:QProcess使用总结

    1 主程序 初始化及设定信号槽 process new QProcess connect process SIGNAL started SLOT started connect process SIGNAL finished int QPr
  • 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】

    02 线性结构3 Reversing Linked List 25 point s Given a constant K and a singly linked list L you are supposed to reverse the