zoukankan      html  css  js  c++  java
  • PAT甲级【2019年9月考题】——A1162 MergingLinkedLists【25】

    7-2 Merging Linked Lists (25 分)

    Given two singly linked lists =...nn  L1=a1→a2→...→an−1→an and =...mm  L2=b1→b2→...→bm−1→bm . If n2n≥2m , you are supposed to reverse and merge the shorter one into the longer one to obtain a list like m..a1→a2→bm→a3→a4→bm−1... For example, given one list being 6→7 and the other one 1→2→3→4→5, you must output 1→2→7→3→4→6→5.

    Input Specification

    Each input file contains one test case. For each case, the first line contains the two addresses of the first nodes of 1  L1 and 2  L2 , plus a positive N(10 N(≤105) which is the total number of nodes given. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

    Then N lines follow, each describes a node in the format:

    Address Data Next

    where Address is the position of the node, Data is a positive integer no more than 10 5  105 , and Next is the position of the next node. It is guaranteed that no list is empty, and the longer list is at least twice as long as the shorter one.

    Output Specification

    For each case, output in order the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.

    Sample Input

    00100 01000 7
    02233 2 34891
    00100 6 00001
    34891 3 10086
    01000 1 02233
    00033 5 -1
    10086 4 00033
    00001 7 -1

    Sample Output

    01000 1 02233
    02233 2 00001
    00001 7 34891
    34891 3 10086
    10086 4 00100
    00100 6 00033
    00033 5 -1

    【声明】

      由于此题还未上PAT官网题库,故没有测试集,仅仅是通过了样例,若发现错误,感谢留言指正。

    Solution:

      很简单,就是先把节点数据存储下来,然后分别获取出两条链表的节点,然后进行按要求拼接。

     1 #include <iostream>
     2 #include <vector>
     3 #include <unordered_map>
     4 using namespace std;
     5 struct Node
     6 {
     7     int addr, val, next;
     8 }nodes[100010];
     9 int main()
    10 {
    11     int head, head1, head2, n;
    12     cin >> head1 >> head2 >> n;
    13     vector<Node>v1, v2, res;//存储列表值
    14     for (int i = 0; i < n; ++i)
    15     {
    16         int a, b, c;
    17         cin >> a >> b >> c;
    18         nodes[a] = { a,b,c };
    19     }
    20     for (int p = head1; p != -1; p = nodes[p].next)
    21         v1.push_back(nodes[p]);
    22     for (int p = head2; p != -1; p = nodes[p].next)
    23         v2.push_back(nodes[p]);
    24     if (v1.size() > v2.size())
    25         head = head1;
    26     else
    27     {
    28         v2 = v1;//v2是短边
    29         head = head2;
    30     }
    31     int k = 0;
    32     while (head != -1)
    33     {
    34         res.push_back(nodes[head]);
    35         ++k;
    36         if (k % 2 == 0 && !v2.empty())//两个中间插一个
    37         {
    38             res.push_back(v2.back());
    39             v2.pop_back();
    40         }
    41         head = nodes[head].next;
    42     }
    43     for (int i = 0; i < res.size() - 1; ++i)
    44         printf("%05d %d %05d
    ", res[i].addr, res[i].val, res[i+1].addr);
    45     printf("%05d %d %d
    ", res.back().addr, res.back().val, -1);
    46     return 0;
    47 }


  • 相关阅读:
    Algs4-1.3.44文本编辑器的缓冲区
    Algs4-1.3.42复制栈
    Algs4-1.3.41复制队列采用for方式实现
    Algs4-1.3.39环形缓冲区
    Algs4-1.3.40前移编码
    Algs4-1.3.38删除第k个元素-数组实现
    安全测试的一些漏洞和测试方法
    Java内存自动回收,为什么会有内存泄露?
    三款主流静态源代码安全检测工具比较
    Fortify源码安全检测工具
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11964223.html
Copyright © 2011-2022 走看看