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 ...
随机推荐
- Educational Codeforces Round 41 (Rated for Div. 2)F. k-substrings
题意比较麻烦略 题解:枚举前缀的中点,二分最远能扩展的地方,lcp来check,然后线段树维护每个点最远被覆盖的地方,然后查询线段树即可 //#pragma GCC optimize(2) //#pr ...
- js的中文在网页中显示为乱码
最近的毕业设计写道局部检查用户命是否为空和是否符合规范时 发现页面回显的中文为乱码 then 找到一个和我遇到同样问题的人呐 他说“最近在写一个商城网页的时候遇到了一个问题,那就是javascrip ...
- 【C/C++】C++11 Move, Forward
左值与右值 Lvalue:可以出现在 operator= 左边的 Rvalue:只能出现在operator= 右边的 ; int a = b; a = b; a = a + b; a + b = a; ...
- 德邦总管 修改oracle数据库用户密码的方法
WIN+R打开运行窗口,输入cmd进入命令行: 输入sqlplus ,输入用户名,输入口令(如果是超级管理员SYS的话需在口令之后加上as sysdba)进入sql命令行: 连接成功后,输入“s ...
- hdu6395 (矩阵快速幂+分块)
Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In Hand Onli ...
- Python数据类型——字符串
概论 字符串顾名思义就是一串字符,由于Python中没有“字符”这种数据类型,所以单个的字符也依然是字符串类型的.字符串可以包含一切数据,无论是能从键盘上找到的,还是你根本都不认识的.与数一样,字符串 ...
- fpread for asp.net 应用技术点滴
单元格的相对性应用 //收支计算平衡 RC[-1]当前行的前一列;R[1]C下一行的同一列 R[-2]C[2]当前行的前2行,当前列的后2列 this.FpSpread1.Sheets[0].Ref ...
- DBNavigator1 按钮标题中文 提示中文
http://bbs.2ccc.com/topic.asp?topicid=74320 怎么将DBNavigator显示时是中文标题 来源:本网整理 如何将DBNavigator显示时是中文标题? ...
- js截取固定长度字符串,多余字符显示...
function cutstr(str, len) { var str_length = 0; var str_len = 0; str_cut = new String(); str_len = s ...
- 微信公众号开发前端获取openId
参考 https://blog.csdn.net/qq_35430000/article/details/79299529