[Leedcode] Necessary interview questions for linked lists in data structures (Phase 4)

[Leedcode] Necessary interview questions for linked lists in data structures (Phase 4)



1. Topic

  1. Intersecting linked list: as follows (example):
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。
图示两个链表在节点 c1 开始相交:题目数据 保证 整个链式结构中不存在环。
注意,函数返回结果后,链表必须 保持其原始结构 。

1. Determine whether two linked lists intersect? 2. If it intersects, find the intersection point


insert image description here


insert image description here


insert image description here


2. Idea + diagram

(1) Idea 1

Brute force solution - exhaustive method. Take each node in the A linked list in turn and compare it with all the nodes in the B linked list.
If there are identical nodes, it is an intersection, and the first identical intersection is a common node. The time complexity of doing this is: O(N^2)
So how do we optimize the time complexity to: O(N)


(2) Idea 2

1. If the end nodes are the same, they intersect, otherwise they will not intersect.
2. Find the intersection point: the long linked list takes (length difference) steps first, and then walks at the same time. The first identical node is the intersection point
, as shown in the figure below


insert image description here


Note here: You can use lenA and lenB to calculate the length of the two linked lists, which is convenient for finding the position of the intersection point , as shown in the figure below
insert image description here


insert image description here


insert image description here


3. Source code

The code is as follows (example):

struct ListNode 
{
    
    
     int val;
     struct ListNode *next;
};
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
{
    
    
    struct ListNode* pheadA = headA;
    struct ListNode* pheadB = headB;
    //先判断是否为环形结构
    int lenA = 1;
    while(pheadA -> next)
    {
    
    
        lenA++;
        pheadA = pheadA -> next;
    }
    int lenB = 1;
    while(pheadB -> next)
    {
    
    
        lenB++;
        pheadB = pheadB -> next;
    }
    if(pheadA != pheadB)
    {
    
    
        return NULL;
    }
    int sub = abs(lenA - lenB);
    struct ListNode* longlist = headA;
    struct ListNode* shortlist = headB;
    if(lenA < lenB)
    {
    
    
        longlist = headB;
        shortlist = headA;
    }
    //长的先走sub步
    while(sub--)
    {
    
    
        longlist = longlist -> next;
    }
    //俩个开始一起走
    while(longlist != shortlist)
    {
    
    
        longlist = longlist -> next;
        shortlist = shortlist -> next;
    }
    return longlist;
}

Summarize

The above is what I want to talk about today. This article introduces the necessary interview questions for linked lists in data structures (Phase 4).
If my blog is helpful to you, remember to support it three times. Thank you for your support!
insert image description here

Guess you like

Origin blog.csdn.net/2201_75587702/article/details/129193276