当前位置:网站首页>4273. 链表合并(DAY 64)
4273. 链表合并(DAY 64)
2022-07-18 02:13:00 【张学恒】
1: 题目
给定两个单链表 L1=a1→a2→…→an−1→an 和 L2=b1→b2→…→bm−1→bm。
如果 n≥2m,你的任务是将较短的那个链表逆序,然后将之并入较长的链表,得到形如 a1→a2→bm→a3→a4→bm−1… 的结果。
例如给定两个链表分别为 6→7 和 1→2→3→4→5,你应该输出 1→2→7→3→4→6→5。
补充
本题中可能包含不在两个单链表中的节点,这些节点无需考虑。
输入格式
输入首先在第一行中给出两个链表 L1 和 L2 的头结点的地址,以及正整数 N,即给定的结点总数。
一个结点的地址是一个 5 位数的非负整数(可能包含前导 0),空地址 NULL 用 −1 表示。
随后 N 行,每行按以下格式给出一个结点的信息:
Address Data Next
其中 Address 是结点的地址,Data 是不超过 105 的正整数,Next 是下一个结点的地址。
题目保证没有空链表,并且较长的链表至少是较短链表的两倍长。
输出格式
按顺序输出结果链表,每个结点占一行,格式与输入相同。
数据范围
1≤N≤105
输入样例:
00100 01000 7
02233 2 34891
00100 6 00001
34891 3 10086
01000 1 02233
00033 5 -1
10086 4 00033
00001 7 -1
输出样例:
01000 1 02233
02233 2 00001
00001 7 34891
34891 3 10086
10086 4 00100
00100 6 00033
00033 5 -1
难度:简单
时/空限制:0.4s / 64MB
总通过数:1673
总尝试数:4062
来源:PAT甲级真题1161
算法标签
2:实现:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 100010;
int h1, h2, n;
int v[N], ne[N];
int main()
{
scanf("%d%d%d", &h1, &h2, &n);
while (n -- )
{
int addr, val, next;
scanf("%d%d%d", &addr, &val, &next);
v[addr] = val, ne[addr] = next;
}
vector<PII> a, b;
for (int i = h1; i != -1; i = ne[i]) a.push_back({
i, v[i]});
for (int i = h2; i != -1; i = ne[i]) b.push_back({
i, v[i]});
if (a.size() < b.size()) swap(a, b);
vector<PII> c;
for (int i = 0, j = b.size() - 1; i < a.size(); i += 2, j -- )
{
c.push_back(a[i]);
if (i + 1 < a.size()) c.push_back(a[i + 1]);
if (j >= 0) c.push_back(b[j]);
}
for (int i = 0; i < c.size(); i ++ )
{
printf("%05d %d ", c[i].x, c[i].y);
if (i + 1 < c.size()) printf("%05d\n", c[i + 1].x);
else puts("-1");
}
return 0;
}
边栏推荐
- Handwritten simple promise code comments
- Uniapp authorized login to obtain user information and code
- 同花顺软件炒股线上怎么免费吗??开户安全吗?
- 使用深度學習制作機器人大腦圖紙
- 使用深度学习制作机器人大脑图纸
- STM32中断梅开二度(一)
- MATLAB学习第四天(决策语句)
- MySQL - unique key constraint for table fields
- MySQL 正则表达式
- Utiliser l'apprentissage profond pour faire des dessins de cerveau robotique
猜你喜欢

Detailed explanation of stop, start and restart commands of Vos services of various versions

论文阅读_医疗NLP_ SMedBERT

JMeter 21 天打卡 day08

Using deep learning to make robot brain drawings

如何下载ScienceDirect(Elsevier)文献的补充材料

Ugui source code analysis - clipperregistry

Uniapp authorized login to obtain user information and code

CH549/CH548学习笔记4 - Timer

CSGO突然返回桌面,并且其他应用无反应,如何不重启关闭

对象内存布局和synchronized锁升级
随机推荐
同花顺软件炒股线上怎么免费吗??开户安全吗?
JMeter 21 day clock in Day12
flex 布局 justify-content:space-between 最后一行左对齐的解决方案
思必驰冲刺科创板:年营收3亿亏3.4亿 阿里与联想之星是股东
JMeter 21 day clock in day13
07.二叉树的高级应用
CANoe:.vmodule文件是什么
同花顺软件炒股线上开户收费吗?开户安全吗?
opencv笔记之读取图像去畸变并保存成新图像
梅科尔工作室-DjangoWeb 应用框架+MySQL数据库第四次培训
各版本VOS服务的停止、启动和重启命令详解
论文阅读_医疗NLP_ SMedBERT
Summary of general test points for file upload
Is it safe to open a fund account online? College students seek guidance
手机开通中信证券账户安全吗?
dried food! Integrated fair cold start recommendation system
啥是图神经网络?
Ugui source code analysis - rectmask2d
06.二叉树及简单应用
All types of code after await are directly thrown into the micro task queue and executed later