zoukankan      html  css  js  c++  java
  • 九度题目1505:两个链表的第一个公共结点


    题目描述:

    输入两个链表,找出它们的第一个公共结点。

    输入:

    输入可能包含多个测试样例。
    对于每个测试案例,输入的第一行为两个整数m和n(1<=m,n<=1000):代表将要输入的两个链表的元素的个数。
    接下来的两行,第一行为第一个链表的所有元素,中间用空格隔开。第二行为第二个链表的所有元素,中间用空格隔开。

    输出:

    对应每个测试案例,
    输出两个链表的第一个公共结点的值。
    如果两个链表没有公共结点,则输出“My God”。

    样例输入:
    5 4
    1 2 3 6 7
    4 5 6 7
    3 3
    1 5 7
    2 4 7
    2 3
    1 3
    4 5 6
    样例输出:
    6
    7
    My God


    #include<iostream>
    #include<limits.h>
    using namespace std;
    
    struct Node
    {
        int val;
        Node *next;
        Node(int value):val(value),next(NULL){}
    };
    
    void list_construct(Node **head, int len)
    {
        int i;
        int val;
        Node *p;
        for(i=0;i<len;++i)
        {
            cin>>val;
            if(NULL==*head)
            {
                *head = new Node(val);
                p = *head;
            }
            else
            {
                p->next = new Node(val); 
                p = p->next;
            }
        }
        return;
    }
    
    void list_print(Node *head)
    {
        while(head)
        {
            cout<<head->val;
            head = head->next;
        }
        return;
    }
    
    
    int get_length(Node *head)
    {
        int len = 0;
        while(head)
        {
            len++;
            head = head->next;
        }
        return len;
    }
    int common_point(Node *list1, Node *list2)
    {
        int len1 = get_length(list1);
        int len2 = get_length(list2);
        Node *p;
        int len;
        if(len1<len2)
        {
            p = list2;
            list2 = list1;
            list1 = p;
            len = len2;
            len2 = len1;
            len1 = len;
            
        }
        len = len1 - len2;
        while(len--)
            list1 = list1->next;
        while(list1||list2)
        {
            if(list1->val==list2->val)
                return list1->val;
            else
            {
                list1 = list1->next;
                list2 = list2->next;
            }
        }
        if(list1==NULL)
            return INT_MIN;
    
        
    }
    int main()
    {
        int m,n;
        Node *list1, *list2;//list must be null,each loop
        int result;
        while(cin>>m>>n)
        {
            list1 = list2 = NULL;    //代码中要求是初始为NULL,否则报错 不初始默认不是NULL
            list_construct(&list1,m); 
            list_construct(&list2,n); 
            //        list_print(list1);
            //       list_print(list2);
           result = common_point(list1,list2); 
           if(result==INT_MIN)
               cout<<"My God"<<endl;
           else
               cout<<result<<endl;
        }
        return 0;
    }
    

    此题难点不在算法,而在于链表的构建与处理上

    1. 构建时,void list_construct(Node **head, int len);

    注意Node **head

    2. 求长度和其它

    两链表长度的处理,把长的放在前面,好控制

    3. 当构建时,两个list头要为空,因为代码是这样写的




    每天早上叫醒你的不是闹钟,而是心中的梦~
  • 相关阅读:
    leetcode Remove Nth Node From End of List
    leetcode Plus One
    leetcode climbing stairs
    leetcode Merge Two Sorted Lists
    leetcode Maximum Subarray
    leetcode Binary Tree Level Order Traversal I II
    leetcode Pascal's Triangle II
    leetcode pascal's triangle
    leetcode valid parentheses
    leetcode Path Sum
  • 原文地址:https://www.cnblogs.com/vintion/p/4116836.html
Copyright © 2011-2022 走看看