题意:给出两条链表的首地址以及若干个节点的的地址、数据、下一个节点的地址,求两条链表的首个共用节点的地址。如果两条链表没有共用节点,则输出-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. PAT (Advanced Level) Practice 1028 List Sorting (25 分) (自定义排序)

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...

  2. jQuery---scrollTop和scrollLeft的方法

    scrollTop和scrollLeft的方法 <script src="jquery-1.12.4.js"></script> <script> ...

  3. 2019 LOL 全球总决赛

                                        FPS 牛逼 涅槃重生

  4. 有一个树形结构,实现一个方法getKeys(data,str),获取字符串str在data中的所有上级节点的名称

    有一个树形结构,实现一个方法getKeys(data,str);获取字符串str在data中的所有上级节点的名称,例如: getKeys(data,'str1') 返回 ‘key1' getKeys( ...

  5. day02_1hibernate

    对象状态与一级缓存 一.对象缓存状态的介绍: ①在使用hibernate时对象的三种状态:(代码如下) 瞬时状态 :没有与session关联,没有主键OID标识(主键的OID指的是对象id,在配置文件 ...

  6. 洛谷P1147 连续自然数和

    https://www.luogu.org/problem/P1147 #include<bits/stdc++.h> using namespace std; int main(){ i ...

  7. Entry小部件:

    导入tkinter import Tkinter from Tinter import * import tkinter from tinter import * 实例化Tk类 root=tkinte ...

  8. 题解【BZOJ4472】[JSOI2015]salesman

    题面 树形\(\text{DP}\)与贪心的结合. 首先考虑树形\(\text{DP}\). 设\(dp_i\)表示从\(i\)出发,访问\(i\)的子树,并且最后回到\(i\)能获得的最大收益. 转 ...

  9. 杭电oj1717——小数化分数(java实现)

    question:小数化分数2 思路: /** * 这道题没有整数部分(有也无所谓,算小数部分,算完了分子分母按倍数加上就好),也就是说数组直接从a[2]开始后面是小数,我把这道题分为了三类: * * ...

  10. vue小例子-01

    1.在components下建一个 2.代码如下: <template> <!--1.业务是开始有一组数据,序号,姓名,性别,年龄,操作(删除)     2.有三个输入框输入姓名,性 ...