First common node of two lists

Source: Internet
Author: User

Title: Enter two linked lists to find their first common node. The linked list is defined as follows:

struct listnode{int m_nkey; Listnode* M_pnext;};

Analysis: Method One, brute force method. Each node is traversed sequentially on the first list, and each node is traversed sequentially on the second list each time it traverses a node. If there is a node on the second list that is the same as the nodes on the first list, the two linked lists overlap on this point, and the first common node is found, the time complexity is O (MN), and the m,n is the length of the two linked list. The time complexity of this method is too high to be considered.

Method Two, the two linked list is traversed sequentially after the node into two stacks, and then start the popup node, compare the top elements of the stack is the same, if the same, then the next top of the stack, until the last one to find the same node, then found the first common node. The time complexity is O (m+n). However, two stacks are used here, which is equivalent to space consumption in exchange for time efficiency. We also have a better way to do this, without the need for an auxiliary stack.

Method Three, first traverse two linked list, get two list length, get two linked list which long, long linked list longer than short chain list a few nodes. Then a second traversal, a long list of steps to make a few moves to the end of the two linked list can reach the end node at the same time, and then at the same time on the two linked lists, found the first of the same node is their first public node. The time complexity is O (m+n). The implementation is as follows:

Listnode* findfirstcommonnode (listnode *phead1,listnode *phead2) {     Unsigned int nlength1=getlistlength (PHEAD1);     unsigned int nlength2= Getlistlength (pHead2);    int nlengthdif=nlength1-nlength2;         ListNode* pListHeadLong=pHead1;    ListNode*  Plistheadshort=phead2;    if (nlength2>nlength1)     {         pListHeadLong=pHead2;         plistheadshort=phead1;        nlengthdif=nlength2-nlength1;     }        for (int i=0;i<nlengthdif;++i)          pListHeadLong=pListHeadLong->m_pNext;            &nbsP;while (plistheadlong!=null) && (plistheadshort!=null) && (plistheadlong!=plistheadshort))      {        pListHeadLong=pListHeadLong->m_pNext;         pListHeadShort=pListHeadShort->m_pNext;     }        ListNode* pFirstCommonNode=pListHeadLong;         return pfirstcommonnode;} Unsigned int getlistlength (Listnode* phead) {    unsigned int  Nlength=0;    listnode* pnode=phead;    while (PNode!=NULL)      {        ++nLength;         pNode=pNode->m_pNext;    }         return nlength;}


This article from "Fairy Road thousands of dust Dream" blog, please be sure to keep this source http://secondscript.blog.51cto.com/9370042/1586719

First common node of two lists

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.