[C + +] Leetcode:60 intersection of the Linked Lists

Source: Internet
Author: User

Topic:

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.

Answer 1: Double-pointer method

ideas: because the subject test instructions refers to the merger, that is, if the two linked list has a common node, the tail node must be the same node, that is, Y-type.

Set two pointers, and if you traverse the table headers from both lists, you obviously cannot find the intersection. However, if the long list is truncated and the two linked lists are traversed at the same time, then the first two pointers of equal nodes are obviously the nodes they are seeking. However, if you go to the end of the list and still do not intersect, the two linked lists do not intersect.

Attention:

1. How to calculate the length of two linked lists.

Iterate through the linked list, recording its length.

while (PA) {pa=pa->next;lengtha++;}   while (Pb) {pb=pb->next;lengthb++;}  
2. You need to save the link header node in case you need to restore the linked list pointer later.
complexity: O (Lengtha + LENGTHB). Space complexity O (1)

AC Code:

< /p>

/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x): Val (x), Next (NULL) {} *}; */class Solution {Public:listnode *getintersectionnode (ListNode *heada, ListNode *headb) {ListNode* PA = HeadA        ;                listnode* PB = headb;        Records the length of Lista and listb int lengtha = 0;        int LENGTHB = 0;            while (PA) {pa = pa->next;        lengtha++;            } while (Pb) {PB = pb->next;        lengthb++;            }//Because the subject test instructions refers to the merger, that is, if the two linked list has a common node, the tail node must be the same node, that is, the Y-type, first move the two linked list to the same number of nodes in the same place if (Lengtha >= LENGTHB) {            int n = LENGTHA-LENGTHB;            PA = Heada;            PB = headb;                while (n) {pa = pa->next;            n--;            }} else {int n = lengthb-lengtha;            PA = Heada;            PB = headb;          while (n)  {PB = pb->next;            n--;            }} while (PA! = PB) {pa = pa->next;        PB = pb->next;    } return PA; }};

Answer 2: Chasing the law (more concise)

Idea: According to the same method of chasing steps, judging if there are two linked list intersection point, the next encounter must be at the intersection, otherwise in the tail node.

algorithm: save two linked list header nodes, synchronously traverse, if a pointer reaches the end, it points to another node of the linked header. Until the same node is available or null.

Complexity: O (Lengtha + LENGTHB)

See below to make it clear.


AC Code:

/** * Definition for singly-linked list. * struct ListNode {*     int val; *     ListNode *next; *     listnode (int x): Val (x), Next (NULL) {} *}; */class Soluti On {public:    listnode *getintersectionnode (ListNode *heada, ListNode *headb) {        listnode* pa = heada;        listnode* PB = headb;                if (PA = = NULL | | pb = = NULL) return null;                while (PA! = null && PB! = NULL && PA! = Pb)        {            pa = pa->next;            PB = pb->next;                        if (PA = = PB) return PA;                        Chase, the next encounter must be at the intersection, if there is no intersection, that is the end point.            if (PA = = NULL) pa = headb;            if (Pb = = NULL) PB = Heada;        }                return PA;    }};



[c++]leetcode:60 intersection of 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.