PAT-链表-A1032 Sharing
题意:给出两条链表的首地址以及若干个节点的的地址、数据、下一个节点的地址,求两条链表的首个共用节点的地址。如果两条链表没有共用节点,则输出-1。
思路:使用静态链表,首先遍历一遍第一个链表并进行标记。然后遍历第二个链表,并检查标记元素,得出结果,进行输出。
代码如下:
```cpp
//所用解法不涉及节点的数据及其地址,
//故不需要在节点中来存储
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1000000;
struct Node
{
//指向下一个地址,标记是否在第一个链表中
int next,flag;
};
Node datas[maxn];
int main()
{
int first1, first2, num;
//读取第一个链表的首地址,第二个链表的首地址,节点数目
scanf("%d %d %d", &first1, &first2, &num);
//获取节点数据
for (int i = 0;i < num;i++)
{
int add_temp, next_temp;
char data_temp;
scanf("%d %c %d", &add_temp, &data_temp, &next_temp);
datas[add_temp].next = next_temp;
}
//遍历第一个链表并进行标记
while (first1 != -1)
{
datas[first1].flag = 1;
first1 = datas[first1].next;
}
//遍历第二个链表,并由标记元素来寻找第一个共同节点
while (first2 != -1 && datas[first2].flag != 1)
{
first2 = datas[first2].next;
}
//输出结果
if (first2 == -1)printf("-1\n");
else printf("%05d\n", first2);
return 0;
}
```
PAT-链表-A1032 Sharing的更多相关文章
- PAT甲级——A1032 Sharing
To store English words, one method is to use linked lists and store a word letter by letter. To save ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- PAT Advanced 1032 Sharing(25) [链表]
题目 To store English words, one method is to use linked lists and store a word letter by letter. To s ...
- PAT A1032 Sharing
题意:给出两条链表的首地址以及若干节点的地址,数据,下一个节点的地址,求两条链表的首个共用节点的地址.如果两条链表没有共用节点,则输出-1.思路步骤1:由于地址的范围很小,因此可以直接用静态链表,但是 ...
- 【PAT】1032 Sharing (25)(25 分)
1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...
- PAT甲 1032. Sharing (25) 2016-09-09 23:13 27人阅读 评论(0) 收藏
1032. Sharing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To store Engl ...
- pat链表专题训练+搜索专题
本期题目包括: 1074:https://pintia.cn/problem-sets/994805342720868352/problems/994805394512134144 1052:http ...
- A1032. Sharing
To store English words, one method is to use linked lists and store a word letter by letter. To save ...
- PAT 甲级 1032 Sharing
https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 To store English words ...
随机推荐
- PAT (Advanced Level) Practice 1005 Spell It Right (20 分) (switch)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
- Ubuntu WiFi连接问题
1,先说明下, 这种方式仅针对 RTL8723BE这款无限网卡. 先用 lspci|grep -i net 先用这个命令查询网卡型号, 我的电脑查询结果: 00:19.0 Ethernet contr ...
- C++ lambda函数及其用法(转)
由于接触C++不久,很多东西比较陌生,今天看阿里云OSS的C++ SDK文件下载部分例子,发现有如下lambda表达式用法,故了解一下相关知识 /*获取文件到本地文件*/ GetObjectReque ...
- vue之项目打包部署到服务器
这是今年的第一篇博客.整理一下vue如何从项目打包到部署服务器,给大家做下分享,希望能给大家带来或多或少的帮助,喜欢的大佬们可以给个小赞,如果有问题也可以一起讨论下. 第一步:这是很关键的一步.打开项 ...
- BandwagonHost VPS CentOS7系统启用EPEL源
BandwagonHost默认安装好了EPEL源,只是默认没有启用,所以很多软件都无法安装.我们需要手动启用一下,才能安装里面的软件. # vim /etc/yum.repos.d/epel.repo ...
- vue配置开发,测试,生产环境api
npm run build 调用开发环境接口,打包开发环境npm run build:test 调用测试环境接口,打包测试环境npm run build:prod 调用生产环境接口,打包生产环境 vu ...
- 3行java代码实现百度站长主动推送
个人博客 地址:http://www.wenhaofan.com/article/push-link-seo 介绍 当网站新增了一个网页之后,此时这个网页是不能够立马被百度收录的,如果想以最快的速度被 ...
- 【vue】 vue跳转页面:router-link/this.$router.push()
1.通过标签<router-link> <router-link to='A'>跳转到A页面</router-link> 2.通过方法 this.$router.p ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- rest_framework:解析器
一.解析器的作用 根据请求头content-type选择对应的解析器对请求体内容进行处理. 有application/json,x-www-form-urlencoded,form-data等格式 二 ...