"Original" Leetcodeoj---intersection of the Linked Lists Problem Solving report (classic intersect linked list find intersection)

Source: Internet
Author: User

Title Address:

https://oj.leetcode.com/problems/intersection-of-two-linked-lists/

Topic content:

Write a program to find the node at which the intersection of the singly linked lists begins.

For example, the following, linked lists:

A:          a1→a2                                        c1→c2→c3                               B:     b1→b2→b3

Begin to intersect at node C1.

Notes:

    • If The linked lists has no intersection at all, return null .
    • The linked lists must retain their original structure after the function returns.
    • You may assume there is no cycles anywhere in the entire linked structure.
    • Your code should preferably run in O (n) time and use only O (1) memory.

Method:

First of all, since it's time complexity O (n), then you can't try it one by one.

Second, since space complexity requires O (1), since it is not possible to use data structures such as stack or unordered_map to find the intersection of the linked list.

Well, we need a cool trick to find the intersection.

First Conversion questions:

Suppose that A and B linked lists intersect, requesting the distance from the intersection point to the node of the A-linked table head. (The so-called distance, is the head node walk several times can arrive)

First look at the specific method of seeking:

0, calculate the length of a linked list Lena

1, calculate the length of the B-linked list LenB

2. Reverse a linked list (key)

3. Recalculate the length of the B-linked list Newlenb

4. Return result = (Newlenb-lenb + lenA-1)/2

Specifically to this problem, you also need to reverse the list of a, because you can not change the original structure of the list. Then re-read the list, return to the first result node is OK.

So how did this concrete approach come about?

Try it yourself and you'll understand. In fact, it is a linked table node in the intersection next to the distribution number, drawing too much trouble, if there is time to fill one, or who do not understand the reply I will fill

All code:

/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode*getintersectionnode (ListNode *heada, ListNode *headb) {        if(Heada = = NULL | | headb = =NULL)returnNULL; intAddressa;//fin of A.        intADDRESSB;//fin of B.        intLenA = Countlength (heada,&Addressa); intLenB = Countlength (headb,&ADDRESSB); if(Addressa! = ADDRESSB)//if has a intersect            returnNULL; ListNode*tmpheada = (ListNode *) Addressa;//To store Heada ' s tail for reverse.Reverselink (Heada); intNewlenb = Countlength (headb,&ADDRESSB); intTonew =Findcount (LENA,LENB,NEWLENB);        Reverselink (Tmpheada); returnFindnthnode (heada,tonew); }        intFindcount (intLenA,intLenB,intNewlenb) {        intGap = Newlenb-LenB; return(Gap + LenA-1) /2; } ListNode*findnthnode (ListNode *head,intToN) {        intCount =0;  while(ToN! =count) {Head= head->Next; Count++; }        returnHead; }        intCountlength (ListNode *head,int*Fin) {        intCount =0;  while(head) {*fin = (int) head; Head= head->Next; Count++; }        returncount; }        voidReverselink (ListNode *head) {ListNode*pre =NULL; ListNode*now =Head; ListNode*NXT = head->Next;  while(1) { now->next =Pre; Pre=Now ; now=NXT; if(NXT) NXT= nxt->Next; Else                 Break; }    }};

"Original" Leetcodeoj---intersection of the Linked Lists Problem Solving report (classic intersect linked list find intersection)

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.