PAT2019年秋季 7-2 Merging Linked Lists (25 分) 经验分享与心路历程

7-2 Merging Linked Lists (25 分)

Given two singly linked lists L​1​​=a​1​​→a​2​​→⋯→a​n−1​​→a​n​​ and L​2​​=b​1​​→b​2​​→⋯→b​m−1​​→b​m​​. If n≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a​1​​→a​2​​→b​m​​→a​3​​→a​4​​→b​m−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 L​1​​and L​2​​, plus a positive N (≤10​5​​) 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​​, 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

 题解:做了40分钟,过样例,这个题目需要多做,但是对自己的自信心,在考场上时间够用的自信心是需要的,大不了我们扔掉一题,只做3道题,拿个70-80分然后其他点稍微扣一点能够拿60分左右。。。主要是考场上面不急是最最重要的事情。。。60分我其实完全是可以接受的,因为有些时候4道题里面总有一道比较坑,那道就别做了。。。其他的拿分再扣一点就好了。。。因为考场上面保持心态把一道道的代码敲完是最最重要的

// 7-2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <bits/stdc++.h>
using namespace std;

struct node
{
    int address;
    int data;
    int next;
};

unordered_map<int, node> input;
vector<node> list1;
vector<node> list2;
vector<node> ans;
int main()
{
#ifndef ONLINE_JUDGE
    FILE* s;
    freopen_s(&s, "in.txt", "r", stdin);
#endif // !ONLINE_JUDGE
    int n,first1, first2;
    cin >> first1>> first2>> n;
    int address;int data;int next;
    for (int i = 0; i < n; i++) {
        cin >> address >> data >> next;
        input[address] = { address,data,next };
    }
    int pos = first1;
    while (input[pos].next != -1) {
        list1.push_back(input[pos]);
        pos = input[pos].next;
    }
    list1.push_back(input[pos]);
    pos = first2;
    while (input[pos].next != -1) {
        list2.push_back(input[pos]);
        pos = input[pos].next;
    }
    list2.push_back(input[pos]);
    //比较他们的长度,把较短的那一条进行翻转并合并
    if (list1.size() > list2.size()) {
        reverse(list2.begin(), list2.end());
        int i,j;
        for (i = 0,j=0;j < list2.size(); i = i + 2,j++) {
            ans.push_back(list1[i]);
            ans.push_back(list1[i+1]);
            ans.push_back(list2[j]);
        }
        for (; i < list1.size(); i++) {
            ans.push_back(list1[i]);
        }
    }
    else {
        reverse(list1.begin(), list1.end());
        int i, j;
        for (i = 0, j = 0; j < list1.size(); i = i + 2, j++) {
            ans.push_back(list2[i]);
            ans.push_back(list2[i + 1]);
            ans.push_back(list1[j]);
        }
        for (; i < list2.size(); i++) {
            ans.push_back(list2[i]);
        }
    }
    for (int i = 0; i < ans.size(); i++) {
        if (i != ans.size() - 1) {
            printf("%05d %d %05d\n", ans[i].address, ans[i].data, ans[i + 1].address);
        }
        else {
            printf("%05d %d -1\n", ans[i].address, ans[i].data);
        }  
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Exodus&Focus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值