zoukankan      html  css  js  c++  java
  • 两个链表的第一个公共结点

    题目

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

    思路

      两个链表有重合,拓扑形状像Y,两个链表有公共结点,那么公共结点出现在链表的尾部,那么从两个链表的最后一个节点开始比较,那么最后一个相同的结点就是我们要找的结点。单链表的最后一个结点,但要最先比较,后进先出,栈!栈!栈!

      1.分别把两个链表放到栈里,尾结点位于栈顶,比较两个栈顶元素是否相同,相同出栈,直到最后一个相同的结点。(当要到达两个链表的尾结点,但是链表的长度不相同,如果从头开始遍历,到达尾结点的时间就不一致。)

      2.先求两个链表的长度,比较他们相差多少结点,再遍历的时候,让长的比短的先走相差的步数,然后再共同向前走,第一个相同的节点就是他们的公共结点。

    #include <iostream>
    using namespace std;
    
    struct node
    {
        int data;
        struct node *next;
        node()
        {
            data=0;
            next=nullptr;
        }
    };
    class Solution
    {
        public:
            Solution();
            void create();
            void print();
            int length() const;
            node *first_repeat_node(const Solution &s);
            node *head;
    };
    Solution::Solution()
    {
        head=new node();
        head->next=nullptr;
        head->data=0;
    }
    void Solution::create()
    {
        if(head==nullptr)
            return;
        auto t=head;
        int x;
        while(1)
        {
            cin>>x;
            if(x)
            {
                t->next=new node();
                t=t->next;
                t->data=x;
                t->next=nullptr;
            }
            else
            {
                t->next=nullptr;
                break;
            }
        }
    } 
    void Solution::print()
    {
        auto n=head;
        while(n)
        {
            cout<<n->data<<endl;
            n=n->next;
        }
    }
    int Solution::length() const
    {
        node *h=head->next;
        int len=0;
        while(h)
        {
            ++len;
            h=h->next;
        }
        return len;
    }
    node *Solution::first_repeat_node(const Solution &s)
    {
        int len1=length();
        int len2=s.length();
        
        node *long_list=head;
        node *short_list=s.head;
        if(len2>len1)
        {
            long_list=s.head;
            short_list=head;
        }
        
        long_list=long_list->next;
        short_list=short_list->next;
        for(int i=0;i<abs(len1-len2);++i)
            long_list=long_list->next;
        while(long_list!=nullptr&&short_list!=nullptr&&long_list->data!=short_list->data)     
        {
            long_list=long_list->next;
            short_list=short_list->next;
        }
        if(long_list->data==short_list->data)
            return long_list;
        else
            return nullptr;
    }
    int main() 
    {
        Solution s,s1;
        s.create();
        s1.create();
        if(s.first_repeat_node(s1))
            cout<<s.first_repeat_node(s1)->data<<endl;
        return 0;
    }

     code

    /*
    struct ListNode {
        int val;
        struct ListNode *next;
        ListNode(int x) :
                val(x), next(NULL) {
        }
    };*/
    class Solution {
    public:
        ListNode* FindFirstCommonNode( ListNode* head1, ListNode* head2) {
            if(!head1||!head2)
                return nullptr;
            
            stack<ListNode *> s1;
            stack<ListNode *> s2;
            ListNode *h1=head1;
            ListNode *h2=head2;
            
            while(h1)
            {
                s1.push(h1);
                h1=h1->next;
            }
            while(h2)
            {
                s2.push(h2);
                h2=h2->next;
            }
            
            ListNode *same=nullptr;
            while(!s1.empty()&&!s2.empty())
            {
                if(s1.top()->val==s2.top()->val)
                {
                    same=s1.top();
                    s1.pop();
                    s2.pop();
                }
                else
                    break;
            }
            
            return same?same:nullptr;
        }
    };
  • 相关阅读:
    Java菜鸟之java基础语法,运算符(三)
    Lotus迁移到Exchange 2010 POC 之Domino Server的配置!
    hibernate:对于java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I错误解决办法
    【targeting学习笔记】Display Advertising Targeting
    Domino 迁移到Exchange 之 Domino Admin 安装!
    sqlite3使用详解(Qt版本)
    .net反混淆脱壳工具de4dot的使用
    破解NET的四大神器(转)
    C#让应用程序只运行一个实例的几种方法
    Windows server 2008 R2实现多用户远程连接
  • 原文地址:https://www.cnblogs.com/tianzeng/p/10260975.html
Copyright © 2011-2022 走看看