The first common node of two lists-enter two linked lists to find their first common node.

Source: Internet
Author: User

1, brute force method:

1 /*2 struct ListNode {3 int val;4 struct ListNode *next;5 listnode (int x):6 val (x), Next (NULL) {7     }8 };*/9 classSolution {Ten  Public: Onelistnode* Findfirstcommonnode (ListNode *phead1, ListNode *pHead2) { A         if(phead1==null| | phead2==NULL) -             returnNULL; -listnode*P1; thelistnode*P2; -          for(p1=phead1;p1!=null;p1=p1->next) { -              for(p2=phead2;p2!=null;p2=p2->next) { -                 if(p1==p2) +                     returnP1; -             } +         } A         returnNULL; at     } -};

2.

From the definition of the list can be seen, the two linked list is a single-linked list, if the two linked list has a common node, then the two linked lists from a node, their m_pnext all point to the same node, then all of their nodes are coincident, it is impossible to fork. So the topology shape looks like the Y type.

One simple way to do this is to iterate through the two lists to get their lengths, to know which list is longer, and how many nodes a long list has than a short list. At the time of the second traversal, take a few steps on a longer node, then traverse through two linked lists at the same time, and the first identical node found is their common node.

1 /*2 struct ListNode {3 int val;4 struct ListNode *next;5 listnode (int x):6 val (x), Next (NULL) {7     }8 };*/9 classSolution {Ten  Public: Onelistnode* Findfirstcommonnode (ListNode *phead1, ListNode *pHead2) { A         if(phead1==null| | phead2==NULL) -             returnNULL; -         intl1=0, l2=0; thelistnode* p1=PHead1; -listnode* p2=pHead2; -          while(p1!=NULL) { -l1++; +P1=p1->Next; -         } +          while(p2!=NULL) { Al2++; atP2=p2->Next; -         } -         intd1=0, d2=0; -         if(l1>L2) -D1=l1-L2; -         if(l1<L2) ind2=l2-L1; -p1=PHead1; toP2=pHead2; +          while(d1!=0){ -P1=p1->Next; thed1--; *         } $          while(d2!=0){Panax NotoginsengP2=p2->Next; -d2--; the         } +          while(p1!=null&&p2!=NULL) { A             if(p1==p2) the                 returnP1; +P1=p1->Next; -P2=p2->Next; $         } $         returnNULL; -     } -};

The first common node of two lists-enter two linked lists to find their first common node.

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.