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行,每行格式为:

  1. Address Data Next

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

输出格式:

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

输入样例:

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

输出样例:

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

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

  1. using namespace std;
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <vector>
  6. #include <queue>
  7. //#include <map>
  8. //#include <set>
  9. #include <sstream>
  10. #include <algorithm>
  11. int N, M, S;
  12. //const int MAXN = , MAXM = 0;
  13. //typedef long long ll;
  14. const int si = , nul = ;
  15. int pre[si], nxt[si], v[si];
  16. int anxt[si];
  17.  
  18. int main() {
  19. cin >> S >> N;
  20. pre[S] = S;
  21. //input
  22. for (int i = ; i < N; i++) {
  23. int adr, val, n;
  24. cin >> adr >> val >> n;
  25. if(n == -) {
  26. n = nul;
  27. }
  28. nxt[adr] = n;
  29. v[adr] = val;
  30. pre[n] = adr;
  31. }
  32. int head = S, back = pre[nul];
  33. int AS = -, fl = ;
  34. int e = , apre;
  35.  
  36. while () {
  37. if (head == back) fl = ;
  38. if (e) {
  39. if (AS == -) {
  40. AS = back;
  41. }
  42. else anxt[apre] = back;
  43. anxt[back] = -;
  44. apre = back;
  45. back = pre[back];
  46. }
  47. else {
  48. anxt[apre] = head;
  49. anxt[head] = -;
  50. apre = head;
  51. head = nxt[head];
  52. }
  53. e = - e;
  54. if (fl) break;
  55. }
  56. //cout << endl;
  57. int crt = AS;
  58. while (crt != -) {
  59. printf("%05d %d ", crt, v[crt]);
  60. if (anxt[crt] != -) {
  61. printf("%05d", anxt[crt]);
  62. }
  63. else {
  64. printf("-1");
  65. }
  66. crt = anxt[crt];
  67. cout << endl;
  68. }
  69. return ;
  70. }

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. wpf 实现 css3 中 boxshadow inset 效果

    using System; using System.Linq; using System.Windows; using System.Windows.Controls; using System.W ...

  2. [bzoj P4504] K个串

    [bzoj P4504] K个串 [题目描述] 兔子们在玩k个串的游戏.首先,它们拿出了一个长度为n的数字序列,选出其中的一个连续子串,然后统计其子串中所有数字之和(注意这里重复出现的数字只被统计一次 ...

  3. F7+vue 物理返回键监听使用

    以前使用的是纯F7,这个项目加了Vue进去,但碰到了一个问题,就是这样监听不到安卓物理键的返回,它是点击返回,直接推出程序,这个坑有点深,查了不少资料也问了不少人,最后在网上看到了别人的写的,自己也改 ...

  4. CloudStack 云计算平台框架

    前言 CloudStack 和OpenStack 一样都是IaaS层 开源框架,可以管理XenServer.ESXI.KVM.OVM等主流虚拟机,相对OpenStack比较简单.稳定: 二.Cloud ...

  5. 数据库 ACID

    ACID是指一个事务本质上有四个特点: Atomicity:原子性 Consistency:一致性 Isolation:隔离性 Durablilty:耐久性 原子性 原子性是指事务是一个不可分割的工作 ...

  6. 数据可视化之 tick_params( 参数 )

    参考:https://blog.csdn.net/helunqu2017/article/details/78736554/ 初学数据可视化,遇到了tick_params() 里面传参数问题,找了一些 ...

  7. Kostya Keygen#2分析

    主要就是构造408ede处的2A个字节.. 其中第一个字节必须为0x2D,倒数第二个字节必须为0x36,倒数第三个字节为0x31. 之后,对这个2A字节的缓冲区,要满足一些条件: 1\ 在408ede ...

  8. 类型限定词——const

    类型限定词有三个:const  volatile restrict. const:一般也叫常量修饰符. 作用:是修饰变量,被修饰的变量就变成常量了,不能被二次修改了. const int a=12:a ...

  9. python dict to dataframe

    http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_dict.html Examples By de ...

  10. Struts中的匹配规则

    <constant name="struts.action.extension" value="action,do,htm"/> 表示之后后缀名为a ...