L2-022 重排链表 (25 分)
给定一个单链表 L1→L2→⋯→Ln−1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln−1→L2→⋯。例如:给定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 分)的更多相关文章
- 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→ ...
- 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 ...
- L2-002 链表去重 (25 分)
L2-002 链表去重 (25 分) 给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉.即对每个键值 K,只有第一个绝对值等于 K 的结点被保留.同时,所有被删除的结点须被保存在 ...
- 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 ( ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- 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 ...
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- 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 ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
随机推荐
- springboot之jackson的两种配置方式
springboot 针对jackson是自动化配置的,如果需要修改,有两种方式: 方式一:通过application.yml 配置属性说明:## spring.jackson.date-format ...
- element-- 修改MessageBox 弹框 中确定和取消按钮顺序
需求:修改弹框中的 取消/确定按钮顺序,及头部和底部背景颜色; 原ui效果图 需求ui效果图 方法:对取消及确定按钮自定义类名,样式重写
- 【算法】祭奠spfa 最短路算法dijspfa
题目链接 本题解来源 其他链接 卡spfa的数据组 题解堆优化的dijkstra 题解spfa讲解 来自以上题解的图片来自常暗踏阴 使用前向星链表存图 直接用队列优化spfa struct cmp { ...
- markdown在线编辑插件mditor
官方地址 https://bh-lay.github.io/mditor/ ##使用方法 #1.页面添加dom ```javascript <textarea id="md_edito ...
- 【Monkey】Monkey获取包名的方式
获取包名的方式: 1.有源码:查看AndroidManifest.xml 2.只有apk: 2.1 aapt dump xmltree ColaBox.apk AndroidManifest.xml ...
- 本地代码上传到git
1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...
- Saiku去掉License验证信息以及数据备份(二十一)
Saiku去掉License验证信息 终于还是走到了这一步,老早就在说要去掉这个License验证了,一直没做因为忙着别的.但是因为这个License还和可定义的用户数相关,限制了我们的使用,所以这里 ...
- Applet
Applet简介: 可以翻译为小应用程序,Java Applet就是用Java语言编写的这样的一些小应用程序,它们可以直接嵌入到网页中,并能够产生特殊的效果.包含Applet的网页被称为Java-po ...
- Java Spring 在线程中或其他位置获取 ApplicationContext 或 ServiceBean
部分一转载自:http://blog.csdn.net/yang123111/article/details/32099329 via @yang123111 部分二转载自:http://www.cn ...
- Python随笔--代理ip