题意:给出两条链表的首地址以及若干个节点的的地址、数据、下一个节点的地址,求两条链表的首个共用节点的地址。如果两条链表没有共用节点,则输出-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的更多相关文章

  1. PAT甲级——A1032 Sharing

    To store English words, one method is to use linked lists and store a word letter by letter. To save ...

  2. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  3. 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 ...

  4. PAT A1032 Sharing

    题意:给出两条链表的首地址以及若干节点的地址,数据,下一个节点的地址,求两条链表的首个共用节点的地址.如果两条链表没有共用节点,则输出-1.思路步骤1:由于地址的范围很小,因此可以直接用静态链表,但是 ...

  5. 【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 ...

  6. 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 ...

  7. pat链表专题训练+搜索专题

    本期题目包括: 1074:https://pintia.cn/problem-sets/994805342720868352/problems/994805394512134144 1052:http ...

  8. A1032. Sharing

    To store English words, one method is to use linked lists and store a word letter by letter. To save ...

  9. PAT 甲级 1032 Sharing

    https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 To store English words ...

随机推荐

  1. win10系统家庭版升级到专业版

    win10家庭版升级专业版密钥:VK7JG-NPHTM-C97JM-9MPGT-3V66T4N7JM-CV98F-WY9XX-9D8CF-369TT FMPND-XFTD4-67FJC-HDR8C-3 ...

  2. Bootstrap4一些零散的知识点

    ·Bootstrap 是全球最受欢迎的前端组件库,用于开发响应式布局.移动设备优先的 WEB 项目. Bootstrap4 目前是 Bootstrap 的最新版本,是一套用于 HTML.CSS 和 J ...

  3. 1级搭建类109-Oracle 12cR2 SI FS(Windows Server 2019)公开

    Oracle 12cR2 单实例文件系统在Windows Server 2019上的安装 在线查看

  4. Oracle Solaris 10 重启后提示 Bad PBR sig

    Solaris 10 安装完毕重启后提示 Bad PBR sig 在磁盘分区的时候,默认自带的 overlap 不要删除,否则启动报错. 分区时,保留overlap(默认显示总容量大小)分区.安装操作 ...

  5. MySQL数据库渗透及漏洞利用总结

    Mysql数据库是目前世界上使用最为广泛的数据库之一,很多著名公司和站点都使用Mysql作为其数据库支撑,目前很多架构都以Mysql作为数据库管理系统,例如LAMP.和WAMP等,在针对网站渗透中,很 ...

  6. contos7 用户管理相关操作命令

    # 查看用户列表 cut -d : -f 1 /etc/passwd # 查看可以登录系统的用户 cat /etc/passwd | grep -v /sbin/nologin | cut -d : ...

  7. P5331 [SNOI2019]通信 [线段树优化建图+最小费用最大流]

    这题真让人自闭-我EK费用流已经死了?- (去掉define int long long就过了) 我建的边害死我的 spfa 还是spfa已经死了? 按费用流的套路来 首先呢 把点 \(i\) 拆成两 ...

  8. python 轮询,长轮询

    轮询相关 用于消息和投票等 轮询 1.采用js 定时请求. html <!DOCTYPE html> <html lang="zh-CN"> <hea ...

  9. 曼孚科技:AI自然语言处理(NLP)领域常用的16个术语

    ​自然语言处理(NLP)是人工智能领域一个十分重要的研究方向.NLP研究的是实现人与计算机之间用自然语言进行有效沟通的各种理论与方法. 本文整理了NLP领域常用的16个术语,希望可以帮助大家更好地理解 ...

  10. Common Subsequence POJ - 1458 最长公共子序列 线性DP

    #include <iostream> #include <algorithm> #include <string> #include <cstring> ...