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. windows+Apache+mod_wsgi+flask部署笔记

    windows是用的2008 server 64位. 照着网上教程即可:https://www.jianshu.com/p/0aa1c7097976 但是有个问题: Apache2.4怎么配置???跟 ...

  2. 在Mac上安装office2016(破解版)

    doffice2016下载地址: 链接:https://pan.baidu.com/s/1V6I_QQiK2VK1rf0v4aVk5g  密码:3ijf 执行破解程序: 链接:https://pan. ...

  3. crontab 每分钟、每小时、每天、每周、每月、每年执行

    每分钟执行 * * * * * 每小时执行 0 * * * * 每天执行 0 0 * * * 每周执行 0 0 * * 0 每月执行 0 0 1 * * 每年执行 0 0 1 1 * 每小时的第3和第 ...

  4. Paper Reading: Stereo DSO

    开篇第一篇就写一个paper reading吧,用markdown+vim写东西切换中英文挺麻烦的,有些就偷懒都用英文写了. Stereo DSO: Large-Scale Direct Sparse ...

  5. sql排列组合

    一个表中4条记录,如何查询所有可能组成的结果. 例如: 1  2  3  4 四条记录,最后组成(12),(13),(14),(23),(24),(34) 最后sql如下: SELECT a.name ...

  6. windows+nginx负载测试

    系统:windows2003nginx版本:1.7.3(官方推荐版本 #父节点 http upstream cluster_1{ ip_hash;#能较好地把同一个客户端的多次请求分配到同一台服务器处 ...

  7. 字符串的顺序倒置。(Reverse)

    实际遇到的问题:在串口获取码表数据的时候,有的码表传到电脑上的数字顺序是颠倒的,即:123.45,会显示为54.321.需要重新处理数据.方法很多,也不难实现,现在列举其中5个. public str ...

  8. leetcode python 041首个缺失正数

    ##限定时间复杂度O(n)num=[0,5,3,1,2,-2,4,8,5,6]num=set(num)d=1for i in range(1,len(num)+1):    if d in num:  ...

  9. Python随笔--继承

  10. jquery实现本地图片上传预览和限流处理

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...