题意:寻找两个链表的首个公共结点,输出其地址。

思路:

方法1.  如果LinkList1比LinkList2长,则让LinkList1先偏移(len1-len2)个结点,然后,让两个链表的的工作指针同时开始偏移,一旦遇到结点地址相同且数据域相同则退出。

方法2(更简洁). 首先遍历LinkList1,每访问一个结点,标记该结点为已经访问;然后,遍历LinkList2,一旦遇到当前结点已经被访问过了,就退出。

代码:

(方法1)

#include <cstdio>
;
struct Node{
    char data;
    int next;
}LinkList[N];

int myStrlen(int head)
{
    ;
    ){
        len++;
        head=LinkList[head].next;
    }
    return len;
}

int main()
{
    //freopen("pat.txt","r",stdin);
    int head1,head2,n;
    scanf("%d%d%d",&head1,&head2,&n);
    int curr,next;
    char data;
    ;i<n;i++){
        scanf("%d %c %d",&curr,&data,&next);
        LinkList[curr].data=data;
        LinkList[curr].next=next;
    }
    int len1=myStrlen(head1);
    int len2=myStrlen(head2);
    int p1=head1,p2=head2;
    if(len1>len2){
        int cnt=len1-len2;
        while(cnt--){
            p1=LinkList[p1].next;
        }
    }else if(len1<len2){
        int cnt=len2-len1;
        while(cnt--){
            p2=LinkList[p2].next;
        }
    }
     && p2!=- ){
        if(LinkList[p1].data==LinkList[p2].data && p1==p2) break;
        p1=LinkList[p1].next;
        p2=LinkList[p2].next;
    }
    ) printf("%d",p1);
    else printf("%05d",p1);
    ;
}

(方法2)

#include <cstdio>

;
struct Node{
    char data;
    int next;
    bool vis;
}LinkList[N];

int main()
{
    //freopen("pat.txt","r",stdin);
    int head1,head2,n;
    scanf("%d%d%d",&head1,&head2,&n);
    int curr,next;
    char data;
    ;i<n;i++){
        scanf("%d %c %d",&curr,&data,&next);
        LinkList[curr].data=data;
        LinkList[curr].next=next;
        LinkList[curr].vis=false;
    }
    int p=head1;
    ){
        LinkList[p].vis=true;
        p=LinkList[p].next;
    }
    p=head2;
    bool flag=false;
    ){
        if(LinkList[p].vis){
            flag=true;
            break;
        }
        p=LinkList[p].next;
    }
    if(flag) printf("%05d",p);
    else printf("-1");
    ;
}

1032 Sharing的更多相关文章

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

  2. PAT 1032 Sharing[hash][链表][一般上]

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

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

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

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

  5. 1032 Sharing (25分)

    1032 Sharing (25分) 题目 思路 定义map存储所有的<地址1,地址2> 第一set存放单词1的所有地址(通过查找map) 通过单词二的首地址,结合map,然后在set中查 ...

  6. 1032. Sharing (25) -set运用

    题目如下: To store English words, one method is to use linked lists and store a word letter by letter. T ...

  7. 1032. Sharing (25)

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

  8. PAT甲题题解-1032. Sharing (25)-链表水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  9. 1032 Sharing (25)(25 point(s))

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

  10. PAT 1032. Sharing

    其实就是链表求交: #include <iostream> #include <cstdio> #include <cstdlib> #include <un ...

随机推荐

  1. LeetCode第[13]题(Java):Roman to Integer

    题目:罗马数字转换 题目难度:easy 题目内容:Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  2. 全国城市部分js

    var areaJson22 = { "id": "0", "name": "全国", "parentId&q ...

  3. 智课雅思词汇---二十三、名词性后缀mony

    智课雅思词汇---二十三.名词性后缀mony 一.总结 一句话总结:Latin: action, result of an action or condition; a suffix that for ...

  4. c#实现验证某个IP地址是否能ping通

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...

  5. 快速掌握Android 虚拟机(AVD)方法

    摘自 http://jingyan.baidu.com/article/15622f24707710fdfdbea553.html 参阅:http://jingyan.baidu.com/articl ...

  6. LeetCode OJ:Sqrt(x)(平方根)

    Implement int sqrt(int x). Compute and return the square root of x. 简单的二分法,注意mid应该选为long,否则容易溢出: cla ...

  7. Agilent RF fundamentals (9)-Mixer basics

    Function: change the frequency of the incident RF key parameters ----------------------------------- ...

  8. canvas - 圆圈内 hover效果

    链接

  9. libQt5Core.so: undefined reference to `dlclose@GLIBC_2.4'

    /******************************************************************************** * libQt5Core.so: u ...

  10. 运行python代码

    IPython IPython 'magic' function documentation