L2-022 重排链表 (25 分)
 

给定一个单链表 L​1​​→L​2​​→⋯→L​n−1​​→L​n​​,请编写程序将链表重新排列为 L​n​​→L​1​​→L​n−1​​→L​2​​→⋯。例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2→4→3。

输入格式:

每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址和结点总个数,即正整数N (≤)。结点的地址是5位非负整数,NULL地址用−表示。

接下来有N行,每行格式为:

Address Data Next

其中Address是结点地址;Data是该结点保存的数据,为不超过1的正整数;Next是下一结点的地址。题目保证给出的链表上至少有两个结点。

输出格式:

对每个测试用例,顺序输出重排后的结果链表,其上每个结点占一行,格式与输入相同。

输入样例:

00100 6
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

输出样例:

68237 6 00100
00100 1 99999
99999 5 12309
12309 2 00000
00000 4 33218
33218 3 -1

链表操作 把-1定义成100040方便倒腾

using namespace std;
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
//#include <map>
//#include <set>
#include <sstream>
#include <algorithm>
int N, M, S;
//const int MAXN = , MAXM = 0;
//typedef long long ll;
const int si = , nul = ;
int pre[si], nxt[si], v[si];
int anxt[si]; int main() {
cin >> S >> N;
pre[S] = S;
//input
for (int i = ; i < N; i++) {
int adr, val, n;
cin >> adr >> val >> n;
if(n == -) {
n = nul;
}
nxt[adr] = n;
v[adr] = val;
pre[n] = adr;
}
int head = S, back = pre[nul];
int AS = -, fl = ;
int e = , apre; while () {
if (head == back) fl = ;
if (e) {
if (AS == -) {
AS = back;
}
else anxt[apre] = back;
anxt[back] = -;
apre = back;
back = pre[back];
}
else {
anxt[apre] = head;
anxt[head] = -;
apre = head;
head = nxt[head];
}
e = - e;
if (fl) break;
}
//cout << endl;
int crt = AS;
while (crt != -) {
printf("%05d %d ", crt, v[crt]);
if (anxt[crt] != -) {
printf("%05d", anxt[crt]);
}
else {
printf("-1");
}
crt = anxt[crt];
cout << endl;
}
return ;
}

L2-022 重排链表 (25 分)的更多相关文章

  1. PAT (Basic Level) Practice (中文)1025 反转链表 (25分)

    1025 反转链表 (25分) 给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转.例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为 3→2→1→6→5→ ...

  2. 1025 反转链表 (25 分)C语言

    题目描述 给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转.例如:给定L为1→2→3→4→5→6,K为3,则输出应该为 3→2→1→6→5→4:如果K为4,则输出应该为4→3→2→1→5 ...

  3. L2-002 链表去重 (25 分)

    L2-002 链表去重 (25 分)   给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉.即对每个键值 K,只有第一个绝对值等于 K 的结点被保留.同时,所有被删除的结点须被保存在 ...

  4. PAT-2019年冬季考试-甲级 7-2 Block Reversing (25分) (链表转置)

    7-2 Block Reversing (25分)   Given a singly linked list L. Let us consider every K nodes as a block ( ...

  5. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  6. PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)

    1074 Reversing Linked List (25 分)   Given a constant K and a singly linked list L, you are supposed ...

  7. PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

    1052 Linked List Sorting (25 分)   A linked list consists of a series of structures, which are not ne ...

  8. PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题

    1043 Is It a Binary Search Tree (25 分)   A Binary Search Tree (BST) is recursively defined as a bina ...

  9. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

随机推荐

  1. Vue-devtools 安装浏览器调试

    工欲善其事,必先利其器. 本文主要讲解Vue-devtools的安装和使用 安装方法有两个: 方法一:(前提条件需要FQ,省事省力省心方便快速) FQ =>谷歌商店 =>搜索 =>V ...

  2. DOM是什么?有什么用处?js与DOM啥关系?

    本文转载于:https://blog.csdn.net/u012155729/article/details/78135393 转载仅供自己后期学习 DOM简介大家都想知道dom是什么,翻了各种文档, ...

  3. idea springboot jrebel hotreloaded

    http://127.0.0.1:8888/88414687-3b91-4286-89ba-2dc813b107ce

  4. CentOS7系统上的LAPACK源码安装

    参考链接:linux下安装blas和lapack BLAS 和 LAPACK 这两个数学库是很多 Linux 科学计算软件需要调用的,所以经常会用到. LAPACK,其名为Linear Algebra ...

  5. javaEE完整体系结构

    学习之前了解熟悉一下javaEE的完整体系结构会更有助于理解 https://segmentfault.com/a/1190000007090110

  6. PlantUML + Chrome 联合使用

    之前都是本地下载安装一个PlantUML,安装过程有点复杂,涉及到的其他插件也有些多. 后面发现Chrome浏览器上提供了相关插件,整个过程简直太流畅了.记录下. 安装: 打开Chrome的线上应用商 ...

  7. Rhino学习教程——1.1

    在Rhino的官网下载好Rhino5.0版本后(Rhino官网会提供下载方式,官网是http://www.xuexiniu.com),双击桌面快捷键,就会出现Rhino的界面(我已经自定义过界面了). ...

  8. bzoj2748

    题解: 简单dp 代码: #include<bits/stdc++.h> using namespace std; ; int n,x,m,a[N],f[N][N]; int main() ...

  9. dede后台登陆不了、出现index.htm Not Found!、无更新模板,解析不了

    以下2个选项内设为空.  

  10. 【转载】Excel 三维地图入门

    三维地图入门(office 2016) https://support.office.com/zh-cn/article/%E4%B8%89%E7%BB%B4%E5%9C%B0%E5%9B%BE%E5 ...