【LeetCode】Data Structure Problem Solution (7) [Intersecting Linked List]

Belonging column: Playing with data structure question types
Blogger's homepage: Chuyang785
Code hosting: chuyang785
Thank you for your support, your likes and attention are the greatest support for me! ! !
Bloggers will also work harder to create better blog posts! !
Follow me, follow me, follow me, say important things three times! ! ! ! ! ! ! !

1. Source of topic

intersecting list

2. Topic description

insert image description here

3. Problem-solving ideas

  • According to the requirements of the topic, we will solve the problem in two steps.
  1. Determine whether to intersect.
  2. Find the intersecting node and return it.
  • First of all, the first step is quite simple. If the two linked lists intersect, the data after the intersecting node must be the same, but we don't know how long the two linked lists are, so we can't read from both linked lists at the same time. The head node of a linked list starts traversing. And we found that if you traverse one by one, you will traverse to the end node, and if they intersect, their end nodes must be the same. So we define two variables curA and curB, traverse the two linked lists respectively, and finally judge whether the tail nodes are the same, if they are the same, they are intersected, otherwise they are not intersected.
  • And our second step is to return the intersection, and we don't know that the lengths of the two linked lists are both small, so is there any way to make their linked lists the same length? So we thought of a way, that is, we first calculate the lengths of the two linked lists, and then calculate the difference between their lengths, and then let the longer linked list walk the length of the difference first, and finally the longer one That linked list is traversed from the node that reaches the difference, and the shorter linked list is traversed from the head node, so the length is the same.

4. Code display

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) 
    {
        if(headA==NULL || headB==NULL)
            return NULL;
        ListNode* curA=headA,*curB=headB;
        //用来计数链表元素的个数
        int countA=0;
        int countB=0;
        //两个链表依次遍历到尾
        while(curA)
        {
            countA++;
            curA=curA->next;
        }
        while(curB)
        {
            countB++;
            curB=curB->next;
        }
        //如果尾相同说明有相交点
        if(curA==curB)
        {
        	//假设headA更长
            ListNode *longList=headA;
            ListNode *shortList=headB;
            //纠正长度
            if(countA<countB)
            {
                longList=headB;
                shortList=headA;
            }
            //计算差值
            int ret=abs(countA-countB);
            //更长的走差值步
            while(ret--)
            {
                longList=longList->next;
            }
            //最后同时遍历,相同则是相交的节点
            while(longList != shortList)
            {
                longList=longList->next;
                shortList=shortList->next;
            }
            return longList;
        }
        else
        {
            return NULL;
        }
    }
};

Guess you like

Origin blog.csdn.net/qq_74276498/article/details/130620713