160. intersection of two linked lists

Source: Internet
Author: User

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

 

For example, the following two linked lists:

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

Begin to intersect at node C1.

 

Notes:

  • If the two linked lists have no intersection at all, returnnull.
  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code shocould preferably run in O (n) time and use only O (1) memory.
    Find the intersection node. It is a simple question. Use a double pointer to record two linked lists. If they are equal, return the node. If not, continue next. If there is no next at the end, return null. When one of the pointers is completed, it is redirected to another linked list, so that the two pointer steps can be consistent. In the second iteration, the intersection node is met. The last time complexity is the sum of the two linked lists. Consider the worst case o (m + n ). For example, a = {1, 2, 4, 5}, B = {6, 7, 8, 3, 4, 5 }. The length of a is smaller than that of B. A goes first, and B goes to the 4 position. A redirects to the 6 position of B in the linked list. B goes to 5 at this time, then B redirects to position 1 of the linked list, and the last two pointers are the same as the number of steps in intersection 3.
     1 /** 2  * Definition for singly-linked list. 3  * struct ListNode { 4  *     int val; 5  *     struct ListNode *next; 6  * }; 7  */ 8 struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) { 9     if(!headA || !headB)    return NULL;10     struct ListNode *pa=headA,*pb=headB;11     while(1){12         if(pa==pb)  13             return pa;14         else if(!pa->next && !pb->next) 15             return NULL;16         else{17             if(pa->next)    18                 pa=pa->next;19             else    20                 pa=headB;21             if(pb->next)22                 pb=pb->next;23             else24                 pb=headA;25         }26     } 27 }

     

    We can see other great gods write more concisely, with the same principle:
     1 /** 2  * Definition for singly-linked list. 3  * struct ListNode { 4  *     int val; 5  *     struct ListNode *next; 6  * }; 7  */ 8 struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) { 9     if(!headA || !headB)    return NULL;10     struct ListNode *pa=headA,*pb=headB;11     while(pa!=pb){12         pa=pa==NULL?headB:pa->next;13         pb=pb==NULL?headA:pb->next;14     }15     return pa;16 }

     

160. intersection of two linked lists

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.