7-2 Merging Linked Lists (25 分)
Given two singly linked lists L1=a1→a2→⋯→an−1→an and L2=b1→b2→⋯→bm−1→bm. If n≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like 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 L1and L2, plus a positive 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 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
题解:做了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;
}