Leetcode 160. Intersecting Linked List

topic

Given the sum of the head nodes of two singly linked lists  headA ,  headB please find and return the starting node where the two singly linked lists intersect. If there is no intersecting node between the two linked lists, return  null . The title data guarantees that there are no loops in the entire chain structure.

Note that the linked list must maintain its original structure after the function returns the result .

Link: 160. Intersecting Linked List - LeetCode

answer

Judging whether two singly linked lists intersect can only be judged by comparing the addresses of the nodes in turn , not by the value of the nodes. First of all, we should make it clear that if two single-linked lists intersect, the shape of the two linked lists can only be Y-shaped , not X-shaped. If it is X-shaped, the intersecting node has two successor nodes, and A singly linked list node can only have one successor node. Therefore, it can be concluded that if two singly linked lists intersect, the addresses of the tail nodes of the two linked lists must be the same; if the addresses of the tail nodes of the two linked lists are not the same, then the two singly linked lists must not intersect .

Therefore, the program can first judge whether the addresses of the end nodes of the two linked lists are the same, and return NULL if they are different; if they are the same, find the intersection node. The method of finding intersection nodes is as follows:

Assuming that the lengths of the two singly linked lists are lenA and lenB respectively, set the two pointers to point to the head nodes of the two linked lists respectively, let the pointer pointing to the longer linked list go abs(lenA-lenB) steps first, and then the two pointers are simultaneously Walk backwards, and compare whether the addresses of the nodes pointed by the pointers are equal while walking. If they are equal, return to the current node. If they are not equal, walk backward at the same time until the first intersection node is found.

code show as below:

struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) 
{
    struct ListNode* tailA = headA;
    struct ListNode* tailB = headB;
    int lenA = 1;
    int lenB = 1;
    while (tailA->next)
    {
        tailA = tailA->next;
        lenA++;
    }
    while (tailB->next)
    {
        tailB = tailB->next;
        lenB++;
    }
    if (tailA != tailB)
        return NULL;

    struct ListNode* longList = headA;
    struct ListNode* shortList = headB;
    if (lenA < lenB)
    {
        longList = headB;
        shortList = headA;
    }
    int gap = abs(lenA - lenB);
    while (gap--)
    {
        longList = longList->next;
    }
    while (longList != shortList)
    {
        longList = longList->next;
        shortList = shortList->next;
    }
    return longList;
}

Note that because the title states that the number of nodes in the linked list is greater than or equal to 1, the case of an empty linked list is not discussed.

Guess you like

Origin blog.csdn.net/minLi_/article/details/131752782