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. Vue2全家桶之二:vue-router(路由)详细教程,看这个就够了

     作者:东西里本文转载于:https://www.jianshu.com/p/514c7588e877来源:简书 转载仅供自己日后看方便.  由于Vue在开发时对路由支持的不足,于是官方补充了vue- ...

  2. SQL Server 创建索引方法

    转自 <SQL Server 创建索引的 5 种方法> 地址:https://www.cnblogs.com/JiangLe/p/4007091.html 前期准备: create tab ...

  3. 爬虫(九)scrapy框架简介和基础应用

    概要 scrapy框架介绍 环境安装 基础使用 一.什么是Scrapy? Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能 ...

  4. StringUtils.defaultIfBlank

    StringUtils.defaultIfBlank在字符串为null,空串或者空白串的时候,返回指定的默认值. org.apache.commons.lang.StringUtils default ...

  5. python-并发初学

    一.操作系统简单介绍 1.多道技术:(重点)系统内可同时容纳多个作业.这些作业放在外存中,组成一个后备队列,系统按一定的调度原则每次从后备作业队列中选取一个或多个作业进入内存运行,运行作业结束.退出运 ...

  6. [数]青蛙的约会&Strange function

    拓展欧几里得:求导&二分 POJ-1061 拓展欧几里得的应用,需要开long long 第一次做这个题的时候进行了毫无用处的找公式(?),是个现在的我看不懂的迷之思路. 第二发的时候还是不明 ...

  7. Visual Studio+VAssistX自动添加注释,函数头注释,文件头注释

    转载:http://blog.csdn.net/xzytl60937234/article/details/70455777 在VAssistX中为C++提供了比较规范注释模板,用这个注释模板为编写的 ...

  8. jedis连接池参数minEvictableIdleTimeMillis和softMinEvictableIdleTimeMillis探索

    我们通常在使用JedisPoolConfig进行连接池配置的时候,minEvictableIdleTimeMillis和softMinEvictableIdleTimeMillis这两个参数经常会不懂 ...

  9. java算法02 - 树

    树是一类重要的非线性结构.而二叉树是一种比较重要的树,接下来我们来了解二叉树的相关内容. 二叉搜索树:每个节点都不比它左子树的任意元素小,而且不比它的右子树的任意元素大. /** * 二叉搜索树 O( ...

  10. 开通blog,记录学习历程

    2017.12.15日,开通blog,用于回忆知识点的记录和整理. 开通本blog主要做以下几点事情: 1.巩固知识点,基础打牢: 2.在基础牢固的基础上,学习流行的框架: 3.在框架牢固的基础上学习 ...